libfs.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * fs/libfs.c
  3. * Library for filesystems writers.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/export.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/slab.h>
  9. #include <linux/mount.h>
  10. #include <linux/vfs.h>
  11. #include <linux/quotaops.h>
  12. #include <linux/mutex.h>
  13. #include <linux/namei.h>
  14. #include <linux/exportfs.h>
  15. #include <linux/writeback.h>
  16. #include <linux/buffer_head.h> /* sync_mapping_buffers */
  17. #include <asm/uaccess.h>
  18. #include "internal.h"
  19. int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
  20. struct kstat *stat)
  21. {
  22. struct inode *inode = d_inode(dentry);
  23. generic_fillattr(inode, stat);
  24. stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
  25. return 0;
  26. }
  27. EXPORT_SYMBOL(simple_getattr);
  28. int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
  29. {
  30. buf->f_type = dentry->d_sb->s_magic;
  31. buf->f_bsize = PAGE_SIZE;
  32. buf->f_namelen = NAME_MAX;
  33. return 0;
  34. }
  35. EXPORT_SYMBOL(simple_statfs);
  36. /*
  37. * Retaining negative dentries for an in-memory filesystem just wastes
  38. * memory and lookup time: arrange for them to be deleted immediately.
  39. */
  40. int always_delete_dentry(const struct dentry *dentry)
  41. {
  42. return 1;
  43. }
  44. EXPORT_SYMBOL(always_delete_dentry);
  45. const struct dentry_operations simple_dentry_operations = {
  46. .d_delete = always_delete_dentry,
  47. };
  48. EXPORT_SYMBOL(simple_dentry_operations);
  49. /*
  50. * Lookup the data. This is trivial - if the dentry didn't already
  51. * exist, we know it is negative. Set d_op to delete negative dentries.
  52. */
  53. struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  54. {
  55. if (dentry->d_name.len > NAME_MAX)
  56. return ERR_PTR(-ENAMETOOLONG);
  57. if (!dentry->d_sb->s_d_op)
  58. d_set_d_op(dentry, &simple_dentry_operations);
  59. d_add(dentry, NULL);
  60. return NULL;
  61. }
  62. EXPORT_SYMBOL(simple_lookup);
  63. int dcache_dir_open(struct inode *inode, struct file *file)
  64. {
  65. file->private_data = d_alloc_cursor(file->f_path.dentry);
  66. return file->private_data ? 0 : -ENOMEM;
  67. }
  68. EXPORT_SYMBOL(dcache_dir_open);
  69. int dcache_dir_close(struct inode *inode, struct file *file)
  70. {
  71. dput(file->private_data);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(dcache_dir_close);
  75. /* parent is locked at least shared */
  76. static struct dentry *next_positive(struct dentry *parent,
  77. struct list_head *from,
  78. int count)
  79. {
  80. unsigned *seq = &parent->d_inode->i_dir_seq, n;
  81. struct dentry *res;
  82. struct list_head *p;
  83. bool skipped;
  84. int i;
  85. retry:
  86. i = count;
  87. skipped = false;
  88. n = smp_load_acquire(seq) & ~1;
  89. res = NULL;
  90. rcu_read_lock();
  91. for (p = from->next; p != &parent->d_subdirs; p = p->next) {
  92. struct dentry *d = list_entry(p, struct dentry, d_child);
  93. if (!simple_positive(d)) {
  94. skipped = true;
  95. } else if (!--i) {
  96. res = d;
  97. break;
  98. }
  99. }
  100. rcu_read_unlock();
  101. if (skipped) {
  102. smp_rmb();
  103. if (unlikely(*seq != n))
  104. goto retry;
  105. }
  106. return res;
  107. }
  108. static void move_cursor(struct dentry *cursor, struct list_head *after)
  109. {
  110. struct dentry *parent = cursor->d_parent;
  111. unsigned n, *seq = &parent->d_inode->i_dir_seq;
  112. spin_lock(&parent->d_lock);
  113. for (;;) {
  114. n = *seq;
  115. if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
  116. break;
  117. cpu_relax();
  118. }
  119. __list_del(cursor->d_child.prev, cursor->d_child.next);
  120. if (after)
  121. list_add(&cursor->d_child, after);
  122. else
  123. list_add_tail(&cursor->d_child, &parent->d_subdirs);
  124. smp_store_release(seq, n + 2);
  125. spin_unlock(&parent->d_lock);
  126. }
  127. loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
  128. {
  129. struct dentry *dentry = file->f_path.dentry;
  130. switch (whence) {
  131. case 1:
  132. offset += file->f_pos;
  133. case 0:
  134. if (offset >= 0)
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. if (offset != file->f_pos) {
  140. file->f_pos = offset;
  141. if (file->f_pos >= 2) {
  142. struct dentry *cursor = file->private_data;
  143. struct dentry *to;
  144. loff_t n = file->f_pos - 2;
  145. inode_lock_shared(dentry->d_inode);
  146. to = next_positive(dentry, &dentry->d_subdirs, n);
  147. move_cursor(cursor, to ? &to->d_child : NULL);
  148. inode_unlock_shared(dentry->d_inode);
  149. }
  150. }
  151. return offset;
  152. }
  153. EXPORT_SYMBOL(dcache_dir_lseek);
  154. /* Relationship between i_mode and the DT_xxx types */
  155. static inline unsigned char dt_type(struct inode *inode)
  156. {
  157. return (inode->i_mode >> 12) & 15;
  158. }
  159. /*
  160. * Directory is locked and all positive dentries in it are safe, since
  161. * for ramfs-type trees they can't go away without unlink() or rmdir(),
  162. * both impossible due to the lock on directory.
  163. */
  164. int dcache_readdir(struct file *file, struct dir_context *ctx)
  165. {
  166. struct dentry *dentry = file->f_path.dentry;
  167. struct dentry *cursor = file->private_data;
  168. struct list_head *p = &cursor->d_child;
  169. struct dentry *next;
  170. bool moved = false;
  171. if (!dir_emit_dots(file, ctx))
  172. return 0;
  173. if (ctx->pos == 2)
  174. p = &dentry->d_subdirs;
  175. while ((next = next_positive(dentry, p, 1)) != NULL) {
  176. if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
  177. d_inode(next)->i_ino, dt_type(d_inode(next))))
  178. break;
  179. moved = true;
  180. p = &next->d_child;
  181. ctx->pos++;
  182. }
  183. if (moved)
  184. move_cursor(cursor, p);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(dcache_readdir);
  188. ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
  189. {
  190. return -EISDIR;
  191. }
  192. EXPORT_SYMBOL(generic_read_dir);
  193. const struct file_operations simple_dir_operations = {
  194. .open = dcache_dir_open,
  195. .release = dcache_dir_close,
  196. .llseek = dcache_dir_lseek,
  197. .read = generic_read_dir,
  198. .iterate_shared = dcache_readdir,
  199. .fsync = noop_fsync,
  200. };
  201. EXPORT_SYMBOL(simple_dir_operations);
  202. const struct inode_operations simple_dir_inode_operations = {
  203. .lookup = simple_lookup,
  204. };
  205. EXPORT_SYMBOL(simple_dir_inode_operations);
  206. static const struct super_operations simple_super_operations = {
  207. .statfs = simple_statfs,
  208. };
  209. /*
  210. * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
  211. * will never be mountable)
  212. */
  213. struct dentry *mount_pseudo_xattr(struct file_system_type *fs_type, char *name,
  214. const struct super_operations *ops, const struct xattr_handler **xattr,
  215. const struct dentry_operations *dops, unsigned long magic)
  216. {
  217. struct super_block *s;
  218. struct dentry *dentry;
  219. struct inode *root;
  220. struct qstr d_name = QSTR_INIT(name, strlen(name));
  221. s = sget(fs_type, NULL, set_anon_super, MS_NOUSER, NULL);
  222. if (IS_ERR(s))
  223. return ERR_CAST(s);
  224. s->s_maxbytes = MAX_LFS_FILESIZE;
  225. s->s_blocksize = PAGE_SIZE;
  226. s->s_blocksize_bits = PAGE_SHIFT;
  227. s->s_magic = magic;
  228. s->s_op = ops ? ops : &simple_super_operations;
  229. s->s_xattr = xattr;
  230. s->s_time_gran = 1;
  231. root = new_inode(s);
  232. if (!root)
  233. goto Enomem;
  234. /*
  235. * since this is the first inode, make it number 1. New inodes created
  236. * after this must take care not to collide with it (by passing
  237. * max_reserved of 1 to iunique).
  238. */
  239. root->i_ino = 1;
  240. root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
  241. root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
  242. dentry = __d_alloc(s, &d_name);
  243. if (!dentry) {
  244. iput(root);
  245. goto Enomem;
  246. }
  247. d_instantiate(dentry, root);
  248. s->s_root = dentry;
  249. s->s_d_op = dops;
  250. s->s_flags |= MS_ACTIVE;
  251. return dget(s->s_root);
  252. Enomem:
  253. deactivate_locked_super(s);
  254. return ERR_PTR(-ENOMEM);
  255. }
  256. EXPORT_SYMBOL(mount_pseudo_xattr);
  257. int simple_open(struct inode *inode, struct file *file)
  258. {
  259. if (inode->i_private)
  260. file->private_data = inode->i_private;
  261. return 0;
  262. }
  263. EXPORT_SYMBOL(simple_open);
  264. int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  265. {
  266. struct inode *inode = d_inode(old_dentry);
  267. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  268. inc_nlink(inode);
  269. ihold(inode);
  270. dget(dentry);
  271. d_instantiate(dentry, inode);
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(simple_link);
  275. int simple_empty(struct dentry *dentry)
  276. {
  277. struct dentry *child;
  278. int ret = 0;
  279. spin_lock(&dentry->d_lock);
  280. list_for_each_entry(child, &dentry->d_subdirs, d_child) {
  281. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  282. if (simple_positive(child)) {
  283. spin_unlock(&child->d_lock);
  284. goto out;
  285. }
  286. spin_unlock(&child->d_lock);
  287. }
  288. ret = 1;
  289. out:
  290. spin_unlock(&dentry->d_lock);
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(simple_empty);
  294. int simple_unlink(struct inode *dir, struct dentry *dentry)
  295. {
  296. struct inode *inode = d_inode(dentry);
  297. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  298. drop_nlink(inode);
  299. dput(dentry);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(simple_unlink);
  303. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  304. {
  305. if (!simple_empty(dentry))
  306. return -ENOTEMPTY;
  307. drop_nlink(d_inode(dentry));
  308. simple_unlink(dir, dentry);
  309. drop_nlink(dir);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL(simple_rmdir);
  313. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  314. struct inode *new_dir, struct dentry *new_dentry,
  315. unsigned int flags)
  316. {
  317. struct inode *inode = d_inode(old_dentry);
  318. int they_are_dirs = d_is_dir(old_dentry);
  319. if (flags & ~RENAME_NOREPLACE)
  320. return -EINVAL;
  321. if (!simple_empty(new_dentry))
  322. return -ENOTEMPTY;
  323. if (d_really_is_positive(new_dentry)) {
  324. simple_unlink(new_dir, new_dentry);
  325. if (they_are_dirs) {
  326. drop_nlink(d_inode(new_dentry));
  327. drop_nlink(old_dir);
  328. }
  329. } else if (they_are_dirs) {
  330. drop_nlink(old_dir);
  331. inc_nlink(new_dir);
  332. }
  333. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  334. new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(simple_rename);
  338. /**
  339. * simple_setattr - setattr for simple filesystem
  340. * @dentry: dentry
  341. * @iattr: iattr structure
  342. *
  343. * Returns 0 on success, -error on failure.
  344. *
  345. * simple_setattr is a simple ->setattr implementation without a proper
  346. * implementation of size changes.
  347. *
  348. * It can either be used for in-memory filesystems or special files
  349. * on simple regular filesystems. Anything that needs to change on-disk
  350. * or wire state on size changes needs its own setattr method.
  351. */
  352. int simple_setattr(struct dentry *dentry, struct iattr *iattr)
  353. {
  354. struct inode *inode = d_inode(dentry);
  355. int error;
  356. error = setattr_prepare(dentry, iattr);
  357. if (error)
  358. return error;
  359. if (iattr->ia_valid & ATTR_SIZE)
  360. truncate_setsize(inode, iattr->ia_size);
  361. setattr_copy(inode, iattr);
  362. mark_inode_dirty(inode);
  363. return 0;
  364. }
  365. EXPORT_SYMBOL(simple_setattr);
  366. int simple_readpage(struct file *file, struct page *page)
  367. {
  368. clear_highpage(page);
  369. flush_dcache_page(page);
  370. SetPageUptodate(page);
  371. unlock_page(page);
  372. return 0;
  373. }
  374. EXPORT_SYMBOL(simple_readpage);
  375. int simple_write_begin(struct file *file, struct address_space *mapping,
  376. loff_t pos, unsigned len, unsigned flags,
  377. struct page **pagep, void **fsdata)
  378. {
  379. struct page *page;
  380. pgoff_t index;
  381. index = pos >> PAGE_SHIFT;
  382. page = grab_cache_page_write_begin(mapping, index, flags);
  383. if (!page)
  384. return -ENOMEM;
  385. *pagep = page;
  386. if (!PageUptodate(page) && (len != PAGE_SIZE)) {
  387. unsigned from = pos & (PAGE_SIZE - 1);
  388. zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
  389. }
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(simple_write_begin);
  393. /**
  394. * simple_write_end - .write_end helper for non-block-device FSes
  395. * @available: See .write_end of address_space_operations
  396. * @file: "
  397. * @mapping: "
  398. * @pos: "
  399. * @len: "
  400. * @copied: "
  401. * @page: "
  402. * @fsdata: "
  403. *
  404. * simple_write_end does the minimum needed for updating a page after writing is
  405. * done. It has the same API signature as the .write_end of
  406. * address_space_operations vector. So it can just be set onto .write_end for
  407. * FSes that don't need any other processing. i_mutex is assumed to be held.
  408. * Block based filesystems should use generic_write_end().
  409. * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
  410. * is not called, so a filesystem that actually does store data in .write_inode
  411. * should extend on what's done here with a call to mark_inode_dirty() in the
  412. * case that i_size has changed.
  413. */
  414. int simple_write_end(struct file *file, struct address_space *mapping,
  415. loff_t pos, unsigned len, unsigned copied,
  416. struct page *page, void *fsdata)
  417. {
  418. struct inode *inode = page->mapping->host;
  419. loff_t last_pos = pos + copied;
  420. /* zero the stale part of the page if we did a short copy */
  421. if (copied < len) {
  422. unsigned from = pos & (PAGE_SIZE - 1);
  423. zero_user(page, from + copied, len - copied);
  424. }
  425. if (!PageUptodate(page))
  426. SetPageUptodate(page);
  427. /*
  428. * No need to use i_size_read() here, the i_size
  429. * cannot change under us because we hold the i_mutex.
  430. */
  431. if (last_pos > inode->i_size)
  432. i_size_write(inode, last_pos);
  433. set_page_dirty(page);
  434. unlock_page(page);
  435. put_page(page);
  436. return copied;
  437. }
  438. EXPORT_SYMBOL(simple_write_end);
  439. /*
  440. * the inodes created here are not hashed. If you use iunique to generate
  441. * unique inode values later for this filesystem, then you must take care
  442. * to pass it an appropriate max_reserved value to avoid collisions.
  443. */
  444. int simple_fill_super(struct super_block *s, unsigned long magic,
  445. struct tree_descr *files)
  446. {
  447. struct inode *inode;
  448. struct dentry *root;
  449. struct dentry *dentry;
  450. int i;
  451. s->s_blocksize = PAGE_SIZE;
  452. s->s_blocksize_bits = PAGE_SHIFT;
  453. s->s_magic = magic;
  454. s->s_op = &simple_super_operations;
  455. s->s_time_gran = 1;
  456. inode = new_inode(s);
  457. if (!inode)
  458. return -ENOMEM;
  459. /*
  460. * because the root inode is 1, the files array must not contain an
  461. * entry at index 1
  462. */
  463. inode->i_ino = 1;
  464. inode->i_mode = S_IFDIR | 0755;
  465. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  466. inode->i_op = &simple_dir_inode_operations;
  467. inode->i_fop = &simple_dir_operations;
  468. set_nlink(inode, 2);
  469. root = d_make_root(inode);
  470. if (!root)
  471. return -ENOMEM;
  472. for (i = 0; !files->name || files->name[0]; i++, files++) {
  473. if (!files->name)
  474. continue;
  475. /* warn if it tries to conflict with the root inode */
  476. if (unlikely(i == 1))
  477. printk(KERN_WARNING "%s: %s passed in a files array"
  478. "with an index of 1!\n", __func__,
  479. s->s_type->name);
  480. dentry = d_alloc_name(root, files->name);
  481. if (!dentry)
  482. goto out;
  483. inode = new_inode(s);
  484. if (!inode) {
  485. dput(dentry);
  486. goto out;
  487. }
  488. inode->i_mode = S_IFREG | files->mode;
  489. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  490. inode->i_fop = files->ops;
  491. inode->i_ino = i;
  492. d_add(dentry, inode);
  493. }
  494. s->s_root = root;
  495. return 0;
  496. out:
  497. d_genocide(root);
  498. shrink_dcache_parent(root);
  499. dput(root);
  500. return -ENOMEM;
  501. }
  502. EXPORT_SYMBOL(simple_fill_super);
  503. static DEFINE_SPINLOCK(pin_fs_lock);
  504. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  505. {
  506. struct vfsmount *mnt = NULL;
  507. spin_lock(&pin_fs_lock);
  508. if (unlikely(!*mount)) {
  509. spin_unlock(&pin_fs_lock);
  510. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL);
  511. if (IS_ERR(mnt))
  512. return PTR_ERR(mnt);
  513. spin_lock(&pin_fs_lock);
  514. if (!*mount)
  515. *mount = mnt;
  516. }
  517. mntget(*mount);
  518. ++*count;
  519. spin_unlock(&pin_fs_lock);
  520. mntput(mnt);
  521. return 0;
  522. }
  523. EXPORT_SYMBOL(simple_pin_fs);
  524. void simple_release_fs(struct vfsmount **mount, int *count)
  525. {
  526. struct vfsmount *mnt;
  527. spin_lock(&pin_fs_lock);
  528. mnt = *mount;
  529. if (!--*count)
  530. *mount = NULL;
  531. spin_unlock(&pin_fs_lock);
  532. mntput(mnt);
  533. }
  534. EXPORT_SYMBOL(simple_release_fs);
  535. /**
  536. * simple_read_from_buffer - copy data from the buffer to user space
  537. * @to: the user space buffer to read to
  538. * @count: the maximum number of bytes to read
  539. * @ppos: the current position in the buffer
  540. * @from: the buffer to read from
  541. * @available: the size of the buffer
  542. *
  543. * The simple_read_from_buffer() function reads up to @count bytes from the
  544. * buffer @from at offset @ppos into the user space address starting at @to.
  545. *
  546. * On success, the number of bytes read is returned and the offset @ppos is
  547. * advanced by this number, or negative value is returned on error.
  548. **/
  549. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  550. const void *from, size_t available)
  551. {
  552. loff_t pos = *ppos;
  553. size_t ret;
  554. if (pos < 0)
  555. return -EINVAL;
  556. if (pos >= available || !count)
  557. return 0;
  558. if (count > available - pos)
  559. count = available - pos;
  560. ret = copy_to_user(to, from + pos, count);
  561. if (ret == count)
  562. return -EFAULT;
  563. count -= ret;
  564. *ppos = pos + count;
  565. return count;
  566. }
  567. EXPORT_SYMBOL(simple_read_from_buffer);
  568. /**
  569. * simple_write_to_buffer - copy data from user space to the buffer
  570. * @to: the buffer to write to
  571. * @available: the size of the buffer
  572. * @ppos: the current position in the buffer
  573. * @from: the user space buffer to read from
  574. * @count: the maximum number of bytes to read
  575. *
  576. * The simple_write_to_buffer() function reads up to @count bytes from the user
  577. * space address starting at @from into the buffer @to at offset @ppos.
  578. *
  579. * On success, the number of bytes written is returned and the offset @ppos is
  580. * advanced by this number, or negative value is returned on error.
  581. **/
  582. ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  583. const void __user *from, size_t count)
  584. {
  585. loff_t pos = *ppos;
  586. size_t res;
  587. if (pos < 0)
  588. return -EINVAL;
  589. if (pos >= available || !count)
  590. return 0;
  591. if (count > available - pos)
  592. count = available - pos;
  593. res = copy_from_user(to + pos, from, count);
  594. if (res == count)
  595. return -EFAULT;
  596. count -= res;
  597. *ppos = pos + count;
  598. return count;
  599. }
  600. EXPORT_SYMBOL(simple_write_to_buffer);
  601. /**
  602. * memory_read_from_buffer - copy data from the buffer
  603. * @to: the kernel space buffer to read to
  604. * @count: the maximum number of bytes to read
  605. * @ppos: the current position in the buffer
  606. * @from: the buffer to read from
  607. * @available: the size of the buffer
  608. *
  609. * The memory_read_from_buffer() function reads up to @count bytes from the
  610. * buffer @from at offset @ppos into the kernel space address starting at @to.
  611. *
  612. * On success, the number of bytes read is returned and the offset @ppos is
  613. * advanced by this number, or negative value is returned on error.
  614. **/
  615. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  616. const void *from, size_t available)
  617. {
  618. loff_t pos = *ppos;
  619. if (pos < 0)
  620. return -EINVAL;
  621. if (pos >= available)
  622. return 0;
  623. if (count > available - pos)
  624. count = available - pos;
  625. memcpy(to, from + pos, count);
  626. *ppos = pos + count;
  627. return count;
  628. }
  629. EXPORT_SYMBOL(memory_read_from_buffer);
  630. /*
  631. * Transaction based IO.
  632. * The file expects a single write which triggers the transaction, and then
  633. * possibly a read which collects the result - which is stored in a
  634. * file-local buffer.
  635. */
  636. void simple_transaction_set(struct file *file, size_t n)
  637. {
  638. struct simple_transaction_argresp *ar = file->private_data;
  639. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  640. /*
  641. * The barrier ensures that ar->size will really remain zero until
  642. * ar->data is ready for reading.
  643. */
  644. smp_mb();
  645. ar->size = n;
  646. }
  647. EXPORT_SYMBOL(simple_transaction_set);
  648. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  649. {
  650. struct simple_transaction_argresp *ar;
  651. static DEFINE_SPINLOCK(simple_transaction_lock);
  652. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  653. return ERR_PTR(-EFBIG);
  654. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  655. if (!ar)
  656. return ERR_PTR(-ENOMEM);
  657. spin_lock(&simple_transaction_lock);
  658. /* only one write allowed per open */
  659. if (file->private_data) {
  660. spin_unlock(&simple_transaction_lock);
  661. free_page((unsigned long)ar);
  662. return ERR_PTR(-EBUSY);
  663. }
  664. file->private_data = ar;
  665. spin_unlock(&simple_transaction_lock);
  666. if (copy_from_user(ar->data, buf, size))
  667. return ERR_PTR(-EFAULT);
  668. return ar->data;
  669. }
  670. EXPORT_SYMBOL(simple_transaction_get);
  671. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  672. {
  673. struct simple_transaction_argresp *ar = file->private_data;
  674. if (!ar)
  675. return 0;
  676. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  677. }
  678. EXPORT_SYMBOL(simple_transaction_read);
  679. int simple_transaction_release(struct inode *inode, struct file *file)
  680. {
  681. free_page((unsigned long)file->private_data);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(simple_transaction_release);
  685. /* Simple attribute files */
  686. struct simple_attr {
  687. int (*get)(void *, u64 *);
  688. int (*set)(void *, u64);
  689. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  690. char set_buf[24];
  691. void *data;
  692. const char *fmt; /* format for read operation */
  693. struct mutex mutex; /* protects access to these buffers */
  694. };
  695. /* simple_attr_open is called by an actual attribute open file operation
  696. * to set the attribute specific access operations. */
  697. int simple_attr_open(struct inode *inode, struct file *file,
  698. int (*get)(void *, u64 *), int (*set)(void *, u64),
  699. const char *fmt)
  700. {
  701. struct simple_attr *attr;
  702. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  703. if (!attr)
  704. return -ENOMEM;
  705. attr->get = get;
  706. attr->set = set;
  707. attr->data = inode->i_private;
  708. attr->fmt = fmt;
  709. mutex_init(&attr->mutex);
  710. file->private_data = attr;
  711. return nonseekable_open(inode, file);
  712. }
  713. EXPORT_SYMBOL_GPL(simple_attr_open);
  714. int simple_attr_release(struct inode *inode, struct file *file)
  715. {
  716. kfree(file->private_data);
  717. return 0;
  718. }
  719. EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
  720. /* read from the buffer that is filled with the get function */
  721. ssize_t simple_attr_read(struct file *file, char __user *buf,
  722. size_t len, loff_t *ppos)
  723. {
  724. struct simple_attr *attr;
  725. size_t size;
  726. ssize_t ret;
  727. attr = file->private_data;
  728. if (!attr->get)
  729. return -EACCES;
  730. ret = mutex_lock_interruptible(&attr->mutex);
  731. if (ret)
  732. return ret;
  733. if (*ppos) { /* continued read */
  734. size = strlen(attr->get_buf);
  735. } else { /* first read */
  736. u64 val;
  737. ret = attr->get(attr->data, &val);
  738. if (ret)
  739. goto out;
  740. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  741. attr->fmt, (unsigned long long)val);
  742. }
  743. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  744. out:
  745. mutex_unlock(&attr->mutex);
  746. return ret;
  747. }
  748. EXPORT_SYMBOL_GPL(simple_attr_read);
  749. /* interpret the buffer as a number to call the set function with */
  750. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  751. size_t len, loff_t *ppos)
  752. {
  753. struct simple_attr *attr;
  754. u64 val;
  755. size_t size;
  756. ssize_t ret;
  757. attr = file->private_data;
  758. if (!attr->set)
  759. return -EACCES;
  760. ret = mutex_lock_interruptible(&attr->mutex);
  761. if (ret)
  762. return ret;
  763. ret = -EFAULT;
  764. size = min(sizeof(attr->set_buf) - 1, len);
  765. if (copy_from_user(attr->set_buf, buf, size))
  766. goto out;
  767. attr->set_buf[size] = '\0';
  768. val = simple_strtoll(attr->set_buf, NULL, 0);
  769. ret = attr->set(attr->data, val);
  770. if (ret == 0)
  771. ret = len; /* on success, claim we got the whole input */
  772. out:
  773. mutex_unlock(&attr->mutex);
  774. return ret;
  775. }
  776. EXPORT_SYMBOL_GPL(simple_attr_write);
  777. /**
  778. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  779. * @sb: filesystem to do the file handle conversion on
  780. * @fid: file handle to convert
  781. * @fh_len: length of the file handle in bytes
  782. * @fh_type: type of file handle
  783. * @get_inode: filesystem callback to retrieve inode
  784. *
  785. * This function decodes @fid as long as it has one of the well-known
  786. * Linux filehandle types and calls @get_inode on it to retrieve the
  787. * inode for the object specified in the file handle.
  788. */
  789. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  790. int fh_len, int fh_type, struct inode *(*get_inode)
  791. (struct super_block *sb, u64 ino, u32 gen))
  792. {
  793. struct inode *inode = NULL;
  794. if (fh_len < 2)
  795. return NULL;
  796. switch (fh_type) {
  797. case FILEID_INO32_GEN:
  798. case FILEID_INO32_GEN_PARENT:
  799. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  800. break;
  801. }
  802. return d_obtain_alias(inode);
  803. }
  804. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  805. /**
  806. * generic_fh_to_parent - generic helper for the fh_to_parent export operation
  807. * @sb: filesystem to do the file handle conversion on
  808. * @fid: file handle to convert
  809. * @fh_len: length of the file handle in bytes
  810. * @fh_type: type of file handle
  811. * @get_inode: filesystem callback to retrieve inode
  812. *
  813. * This function decodes @fid as long as it has one of the well-known
  814. * Linux filehandle types and calls @get_inode on it to retrieve the
  815. * inode for the _parent_ object specified in the file handle if it
  816. * is specified in the file handle, or NULL otherwise.
  817. */
  818. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  819. int fh_len, int fh_type, struct inode *(*get_inode)
  820. (struct super_block *sb, u64 ino, u32 gen))
  821. {
  822. struct inode *inode = NULL;
  823. if (fh_len <= 2)
  824. return NULL;
  825. switch (fh_type) {
  826. case FILEID_INO32_GEN_PARENT:
  827. inode = get_inode(sb, fid->i32.parent_ino,
  828. (fh_len > 3 ? fid->i32.parent_gen : 0));
  829. break;
  830. }
  831. return d_obtain_alias(inode);
  832. }
  833. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  834. /**
  835. * __generic_file_fsync - generic fsync implementation for simple filesystems
  836. *
  837. * @file: file to synchronize
  838. * @start: start offset in bytes
  839. * @end: end offset in bytes (inclusive)
  840. * @datasync: only synchronize essential metadata if true
  841. *
  842. * This is a generic implementation of the fsync method for simple
  843. * filesystems which track all non-inode metadata in the buffers list
  844. * hanging off the address_space structure.
  845. */
  846. int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
  847. int datasync)
  848. {
  849. struct inode *inode = file->f_mapping->host;
  850. int err;
  851. int ret;
  852. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  853. if (err)
  854. return err;
  855. inode_lock(inode);
  856. ret = sync_mapping_buffers(inode->i_mapping);
  857. if (!(inode->i_state & I_DIRTY_ALL))
  858. goto out;
  859. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  860. goto out;
  861. err = sync_inode_metadata(inode, 1);
  862. if (ret == 0)
  863. ret = err;
  864. out:
  865. inode_unlock(inode);
  866. return ret;
  867. }
  868. EXPORT_SYMBOL(__generic_file_fsync);
  869. /**
  870. * generic_file_fsync - generic fsync implementation for simple filesystems
  871. * with flush
  872. * @file: file to synchronize
  873. * @start: start offset in bytes
  874. * @end: end offset in bytes (inclusive)
  875. * @datasync: only synchronize essential metadata if true
  876. *
  877. */
  878. int generic_file_fsync(struct file *file, loff_t start, loff_t end,
  879. int datasync)
  880. {
  881. struct inode *inode = file->f_mapping->host;
  882. int err;
  883. err = __generic_file_fsync(file, start, end, datasync);
  884. if (err)
  885. return err;
  886. return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  887. }
  888. EXPORT_SYMBOL(generic_file_fsync);
  889. /**
  890. * generic_check_addressable - Check addressability of file system
  891. * @blocksize_bits: log of file system block size
  892. * @num_blocks: number of blocks in file system
  893. *
  894. * Determine whether a file system with @num_blocks blocks (and a
  895. * block size of 2**@blocksize_bits) is addressable by the sector_t
  896. * and page cache of the system. Return 0 if so and -EFBIG otherwise.
  897. */
  898. int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
  899. {
  900. u64 last_fs_block = num_blocks - 1;
  901. u64 last_fs_page =
  902. last_fs_block >> (PAGE_SHIFT - blocksize_bits);
  903. if (unlikely(num_blocks == 0))
  904. return 0;
  905. if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
  906. return -EINVAL;
  907. if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
  908. (last_fs_page > (pgoff_t)(~0ULL))) {
  909. return -EFBIG;
  910. }
  911. return 0;
  912. }
  913. EXPORT_SYMBOL(generic_check_addressable);
  914. /*
  915. * No-op implementation of ->fsync for in-memory filesystems.
  916. */
  917. int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  918. {
  919. return 0;
  920. }
  921. EXPORT_SYMBOL(noop_fsync);
  922. /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
  923. void kfree_link(void *p)
  924. {
  925. kfree(p);
  926. }
  927. EXPORT_SYMBOL(kfree_link);
  928. /*
  929. * nop .set_page_dirty method so that people can use .page_mkwrite on
  930. * anon inodes.
  931. */
  932. static int anon_set_page_dirty(struct page *page)
  933. {
  934. return 0;
  935. };
  936. /*
  937. * A single inode exists for all anon_inode files. Contrary to pipes,
  938. * anon_inode inodes have no associated per-instance data, so we need
  939. * only allocate one of them.
  940. */
  941. struct inode *alloc_anon_inode(struct super_block *s)
  942. {
  943. static const struct address_space_operations anon_aops = {
  944. .set_page_dirty = anon_set_page_dirty,
  945. };
  946. struct inode *inode = new_inode_pseudo(s);
  947. if (!inode)
  948. return ERR_PTR(-ENOMEM);
  949. inode->i_ino = get_next_ino();
  950. inode->i_mapping->a_ops = &anon_aops;
  951. /*
  952. * Mark the inode dirty from the very beginning,
  953. * that way it will never be moved to the dirty
  954. * list because mark_inode_dirty() will think
  955. * that it already _is_ on the dirty list.
  956. */
  957. inode->i_state = I_DIRTY;
  958. inode->i_mode = S_IRUSR | S_IWUSR;
  959. inode->i_uid = current_fsuid();
  960. inode->i_gid = current_fsgid();
  961. inode->i_flags |= S_PRIVATE;
  962. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  963. return inode;
  964. }
  965. EXPORT_SYMBOL(alloc_anon_inode);
  966. /**
  967. * simple_nosetlease - generic helper for prohibiting leases
  968. * @filp: file pointer
  969. * @arg: type of lease to obtain
  970. * @flp: new lease supplied for insertion
  971. * @priv: private data for lm_setup operation
  972. *
  973. * Generic helper for filesystems that do not wish to allow leases to be set.
  974. * All arguments are ignored and it just returns -EINVAL.
  975. */
  976. int
  977. simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
  978. void **priv)
  979. {
  980. return -EINVAL;
  981. }
  982. EXPORT_SYMBOL(simple_nosetlease);
  983. const char *simple_get_link(struct dentry *dentry, struct inode *inode,
  984. struct delayed_call *done)
  985. {
  986. return inode->i_link;
  987. }
  988. EXPORT_SYMBOL(simple_get_link);
  989. const struct inode_operations simple_symlink_inode_operations = {
  990. .get_link = simple_get_link,
  991. .readlink = generic_readlink
  992. };
  993. EXPORT_SYMBOL(simple_symlink_inode_operations);
  994. /*
  995. * Operations for a permanently empty directory.
  996. */
  997. static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  998. {
  999. return ERR_PTR(-ENOENT);
  1000. }
  1001. static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1002. struct kstat *stat)
  1003. {
  1004. struct inode *inode = d_inode(dentry);
  1005. generic_fillattr(inode, stat);
  1006. return 0;
  1007. }
  1008. static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
  1009. {
  1010. return -EPERM;
  1011. }
  1012. static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
  1013. {
  1014. return -EOPNOTSUPP;
  1015. }
  1016. static const struct inode_operations empty_dir_inode_operations = {
  1017. .lookup = empty_dir_lookup,
  1018. .permission = generic_permission,
  1019. .setattr = empty_dir_setattr,
  1020. .getattr = empty_dir_getattr,
  1021. .listxattr = empty_dir_listxattr,
  1022. };
  1023. static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
  1024. {
  1025. /* An empty directory has two entries . and .. at offsets 0 and 1 */
  1026. return generic_file_llseek_size(file, offset, whence, 2, 2);
  1027. }
  1028. static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
  1029. {
  1030. dir_emit_dots(file, ctx);
  1031. return 0;
  1032. }
  1033. static const struct file_operations empty_dir_operations = {
  1034. .llseek = empty_dir_llseek,
  1035. .read = generic_read_dir,
  1036. .iterate_shared = empty_dir_readdir,
  1037. .fsync = noop_fsync,
  1038. };
  1039. void make_empty_dir_inode(struct inode *inode)
  1040. {
  1041. set_nlink(inode, 2);
  1042. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  1043. inode->i_uid = GLOBAL_ROOT_UID;
  1044. inode->i_gid = GLOBAL_ROOT_GID;
  1045. inode->i_rdev = 0;
  1046. inode->i_size = 0;
  1047. inode->i_blkbits = PAGE_SHIFT;
  1048. inode->i_blocks = 0;
  1049. inode->i_op = &empty_dir_inode_operations;
  1050. inode->i_opflags &= ~IOP_XATTR;
  1051. inode->i_fop = &empty_dir_operations;
  1052. }
  1053. bool is_empty_dir_inode(struct inode *inode)
  1054. {
  1055. return (inode->i_fop == &empty_dir_operations) &&
  1056. (inode->i_op == &empty_dir_inode_operations);
  1057. }