libfs.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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(struct file_system_type *fs_type, char *name,
  214. const struct super_operations *ops,
  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_time_gran = 1;
  230. root = new_inode(s);
  231. if (!root)
  232. goto Enomem;
  233. /*
  234. * since this is the first inode, make it number 1. New inodes created
  235. * after this must take care not to collide with it (by passing
  236. * max_reserved of 1 to iunique).
  237. */
  238. root->i_ino = 1;
  239. root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
  240. root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
  241. dentry = __d_alloc(s, &d_name);
  242. if (!dentry) {
  243. iput(root);
  244. goto Enomem;
  245. }
  246. d_instantiate(dentry, root);
  247. s->s_root = dentry;
  248. s->s_d_op = dops;
  249. s->s_flags |= MS_ACTIVE;
  250. return dget(s->s_root);
  251. Enomem:
  252. deactivate_locked_super(s);
  253. return ERR_PTR(-ENOMEM);
  254. }
  255. EXPORT_SYMBOL(mount_pseudo);
  256. int simple_open(struct inode *inode, struct file *file)
  257. {
  258. if (inode->i_private)
  259. file->private_data = inode->i_private;
  260. return 0;
  261. }
  262. EXPORT_SYMBOL(simple_open);
  263. int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  264. {
  265. struct inode *inode = d_inode(old_dentry);
  266. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  267. inc_nlink(inode);
  268. ihold(inode);
  269. dget(dentry);
  270. d_instantiate(dentry, inode);
  271. return 0;
  272. }
  273. EXPORT_SYMBOL(simple_link);
  274. int simple_empty(struct dentry *dentry)
  275. {
  276. struct dentry *child;
  277. int ret = 0;
  278. spin_lock(&dentry->d_lock);
  279. list_for_each_entry(child, &dentry->d_subdirs, d_child) {
  280. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  281. if (simple_positive(child)) {
  282. spin_unlock(&child->d_lock);
  283. goto out;
  284. }
  285. spin_unlock(&child->d_lock);
  286. }
  287. ret = 1;
  288. out:
  289. spin_unlock(&dentry->d_lock);
  290. return ret;
  291. }
  292. EXPORT_SYMBOL(simple_empty);
  293. int simple_unlink(struct inode *dir, struct dentry *dentry)
  294. {
  295. struct inode *inode = d_inode(dentry);
  296. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  297. drop_nlink(inode);
  298. dput(dentry);
  299. return 0;
  300. }
  301. EXPORT_SYMBOL(simple_unlink);
  302. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  303. {
  304. if (!simple_empty(dentry))
  305. return -ENOTEMPTY;
  306. drop_nlink(d_inode(dentry));
  307. simple_unlink(dir, dentry);
  308. drop_nlink(dir);
  309. return 0;
  310. }
  311. EXPORT_SYMBOL(simple_rmdir);
  312. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  313. struct inode *new_dir, struct dentry *new_dentry)
  314. {
  315. struct inode *inode = d_inode(old_dentry);
  316. int they_are_dirs = d_is_dir(old_dentry);
  317. if (!simple_empty(new_dentry))
  318. return -ENOTEMPTY;
  319. if (d_really_is_positive(new_dentry)) {
  320. simple_unlink(new_dir, new_dentry);
  321. if (they_are_dirs) {
  322. drop_nlink(d_inode(new_dentry));
  323. drop_nlink(old_dir);
  324. }
  325. } else if (they_are_dirs) {
  326. drop_nlink(old_dir);
  327. inc_nlink(new_dir);
  328. }
  329. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  330. new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
  331. return 0;
  332. }
  333. EXPORT_SYMBOL(simple_rename);
  334. /**
  335. * simple_setattr - setattr for simple filesystem
  336. * @dentry: dentry
  337. * @iattr: iattr structure
  338. *
  339. * Returns 0 on success, -error on failure.
  340. *
  341. * simple_setattr is a simple ->setattr implementation without a proper
  342. * implementation of size changes.
  343. *
  344. * It can either be used for in-memory filesystems or special files
  345. * on simple regular filesystems. Anything that needs to change on-disk
  346. * or wire state on size changes needs its own setattr method.
  347. */
  348. int simple_setattr(struct dentry *dentry, struct iattr *iattr)
  349. {
  350. struct inode *inode = d_inode(dentry);
  351. int error;
  352. error = inode_change_ok(inode, iattr);
  353. if (error)
  354. return error;
  355. if (iattr->ia_valid & ATTR_SIZE)
  356. truncate_setsize(inode, iattr->ia_size);
  357. setattr_copy(inode, iattr);
  358. mark_inode_dirty(inode);
  359. return 0;
  360. }
  361. EXPORT_SYMBOL(simple_setattr);
  362. int simple_readpage(struct file *file, struct page *page)
  363. {
  364. clear_highpage(page);
  365. flush_dcache_page(page);
  366. SetPageUptodate(page);
  367. unlock_page(page);
  368. return 0;
  369. }
  370. EXPORT_SYMBOL(simple_readpage);
  371. int simple_write_begin(struct file *file, struct address_space *mapping,
  372. loff_t pos, unsigned len, unsigned flags,
  373. struct page **pagep, void **fsdata)
  374. {
  375. struct page *page;
  376. pgoff_t index;
  377. index = pos >> PAGE_SHIFT;
  378. page = grab_cache_page_write_begin(mapping, index, flags);
  379. if (!page)
  380. return -ENOMEM;
  381. *pagep = page;
  382. if (!PageUptodate(page) && (len != PAGE_SIZE)) {
  383. unsigned from = pos & (PAGE_SIZE - 1);
  384. zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
  385. }
  386. return 0;
  387. }
  388. EXPORT_SYMBOL(simple_write_begin);
  389. /**
  390. * simple_write_end - .write_end helper for non-block-device FSes
  391. * @available: See .write_end of address_space_operations
  392. * @file: "
  393. * @mapping: "
  394. * @pos: "
  395. * @len: "
  396. * @copied: "
  397. * @page: "
  398. * @fsdata: "
  399. *
  400. * simple_write_end does the minimum needed for updating a page after writing is
  401. * done. It has the same API signature as the .write_end of
  402. * address_space_operations vector. So it can just be set onto .write_end for
  403. * FSes that don't need any other processing. i_mutex is assumed to be held.
  404. * Block based filesystems should use generic_write_end().
  405. * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
  406. * is not called, so a filesystem that actually does store data in .write_inode
  407. * should extend on what's done here with a call to mark_inode_dirty() in the
  408. * case that i_size has changed.
  409. */
  410. int simple_write_end(struct file *file, struct address_space *mapping,
  411. loff_t pos, unsigned len, unsigned copied,
  412. struct page *page, void *fsdata)
  413. {
  414. struct inode *inode = page->mapping->host;
  415. loff_t last_pos = pos + copied;
  416. /* zero the stale part of the page if we did a short copy */
  417. if (copied < len) {
  418. unsigned from = pos & (PAGE_SIZE - 1);
  419. zero_user(page, from + copied, len - copied);
  420. }
  421. if (!PageUptodate(page))
  422. SetPageUptodate(page);
  423. /*
  424. * No need to use i_size_read() here, the i_size
  425. * cannot change under us because we hold the i_mutex.
  426. */
  427. if (last_pos > inode->i_size)
  428. i_size_write(inode, last_pos);
  429. set_page_dirty(page);
  430. unlock_page(page);
  431. put_page(page);
  432. return copied;
  433. }
  434. EXPORT_SYMBOL(simple_write_end);
  435. /*
  436. * the inodes created here are not hashed. If you use iunique to generate
  437. * unique inode values later for this filesystem, then you must take care
  438. * to pass it an appropriate max_reserved value to avoid collisions.
  439. */
  440. int simple_fill_super(struct super_block *s, unsigned long magic,
  441. struct tree_descr *files)
  442. {
  443. struct inode *inode;
  444. struct dentry *root;
  445. struct dentry *dentry;
  446. int i;
  447. s->s_blocksize = PAGE_SIZE;
  448. s->s_blocksize_bits = PAGE_SHIFT;
  449. s->s_magic = magic;
  450. s->s_op = &simple_super_operations;
  451. s->s_time_gran = 1;
  452. inode = new_inode(s);
  453. if (!inode)
  454. return -ENOMEM;
  455. /*
  456. * because the root inode is 1, the files array must not contain an
  457. * entry at index 1
  458. */
  459. inode->i_ino = 1;
  460. inode->i_mode = S_IFDIR | 0755;
  461. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  462. inode->i_op = &simple_dir_inode_operations;
  463. inode->i_fop = &simple_dir_operations;
  464. set_nlink(inode, 2);
  465. root = d_make_root(inode);
  466. if (!root)
  467. return -ENOMEM;
  468. for (i = 0; !files->name || files->name[0]; i++, files++) {
  469. if (!files->name)
  470. continue;
  471. /* warn if it tries to conflict with the root inode */
  472. if (unlikely(i == 1))
  473. printk(KERN_WARNING "%s: %s passed in a files array"
  474. "with an index of 1!\n", __func__,
  475. s->s_type->name);
  476. dentry = d_alloc_name(root, files->name);
  477. if (!dentry)
  478. goto out;
  479. inode = new_inode(s);
  480. if (!inode) {
  481. dput(dentry);
  482. goto out;
  483. }
  484. inode->i_mode = S_IFREG | files->mode;
  485. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  486. inode->i_fop = files->ops;
  487. inode->i_ino = i;
  488. d_add(dentry, inode);
  489. }
  490. s->s_root = root;
  491. return 0;
  492. out:
  493. d_genocide(root);
  494. shrink_dcache_parent(root);
  495. dput(root);
  496. return -ENOMEM;
  497. }
  498. EXPORT_SYMBOL(simple_fill_super);
  499. static DEFINE_SPINLOCK(pin_fs_lock);
  500. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  501. {
  502. struct vfsmount *mnt = NULL;
  503. spin_lock(&pin_fs_lock);
  504. if (unlikely(!*mount)) {
  505. spin_unlock(&pin_fs_lock);
  506. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL);
  507. if (IS_ERR(mnt))
  508. return PTR_ERR(mnt);
  509. spin_lock(&pin_fs_lock);
  510. if (!*mount)
  511. *mount = mnt;
  512. }
  513. mntget(*mount);
  514. ++*count;
  515. spin_unlock(&pin_fs_lock);
  516. mntput(mnt);
  517. return 0;
  518. }
  519. EXPORT_SYMBOL(simple_pin_fs);
  520. void simple_release_fs(struct vfsmount **mount, int *count)
  521. {
  522. struct vfsmount *mnt;
  523. spin_lock(&pin_fs_lock);
  524. mnt = *mount;
  525. if (!--*count)
  526. *mount = NULL;
  527. spin_unlock(&pin_fs_lock);
  528. mntput(mnt);
  529. }
  530. EXPORT_SYMBOL(simple_release_fs);
  531. /**
  532. * simple_read_from_buffer - copy data from the buffer to user space
  533. * @to: the user space buffer to read to
  534. * @count: the maximum number of bytes to read
  535. * @ppos: the current position in the buffer
  536. * @from: the buffer to read from
  537. * @available: the size of the buffer
  538. *
  539. * The simple_read_from_buffer() function reads up to @count bytes from the
  540. * buffer @from at offset @ppos into the user space address starting at @to.
  541. *
  542. * On success, the number of bytes read is returned and the offset @ppos is
  543. * advanced by this number, or negative value is returned on error.
  544. **/
  545. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  546. const void *from, size_t available)
  547. {
  548. loff_t pos = *ppos;
  549. size_t ret;
  550. if (pos < 0)
  551. return -EINVAL;
  552. if (pos >= available || !count)
  553. return 0;
  554. if (count > available - pos)
  555. count = available - pos;
  556. ret = copy_to_user(to, from + pos, count);
  557. if (ret == count)
  558. return -EFAULT;
  559. count -= ret;
  560. *ppos = pos + count;
  561. return count;
  562. }
  563. EXPORT_SYMBOL(simple_read_from_buffer);
  564. /**
  565. * simple_write_to_buffer - copy data from user space to the buffer
  566. * @to: the buffer to write to
  567. * @available: the size of the buffer
  568. * @ppos: the current position in the buffer
  569. * @from: the user space buffer to read from
  570. * @count: the maximum number of bytes to read
  571. *
  572. * The simple_write_to_buffer() function reads up to @count bytes from the user
  573. * space address starting at @from into the buffer @to at offset @ppos.
  574. *
  575. * On success, the number of bytes written is returned and the offset @ppos is
  576. * advanced by this number, or negative value is returned on error.
  577. **/
  578. ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  579. const void __user *from, size_t count)
  580. {
  581. loff_t pos = *ppos;
  582. size_t res;
  583. if (pos < 0)
  584. return -EINVAL;
  585. if (pos >= available || !count)
  586. return 0;
  587. if (count > available - pos)
  588. count = available - pos;
  589. res = copy_from_user(to + pos, from, count);
  590. if (res == count)
  591. return -EFAULT;
  592. count -= res;
  593. *ppos = pos + count;
  594. return count;
  595. }
  596. EXPORT_SYMBOL(simple_write_to_buffer);
  597. /**
  598. * memory_read_from_buffer - copy data from the buffer
  599. * @to: the kernel space buffer to read to
  600. * @count: the maximum number of bytes to read
  601. * @ppos: the current position in the buffer
  602. * @from: the buffer to read from
  603. * @available: the size of the buffer
  604. *
  605. * The memory_read_from_buffer() function reads up to @count bytes from the
  606. * buffer @from at offset @ppos into the kernel space address starting at @to.
  607. *
  608. * On success, the number of bytes read is returned and the offset @ppos is
  609. * advanced by this number, or negative value is returned on error.
  610. **/
  611. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  612. const void *from, size_t available)
  613. {
  614. loff_t pos = *ppos;
  615. if (pos < 0)
  616. return -EINVAL;
  617. if (pos >= available)
  618. return 0;
  619. if (count > available - pos)
  620. count = available - pos;
  621. memcpy(to, from + pos, count);
  622. *ppos = pos + count;
  623. return count;
  624. }
  625. EXPORT_SYMBOL(memory_read_from_buffer);
  626. /*
  627. * Transaction based IO.
  628. * The file expects a single write which triggers the transaction, and then
  629. * possibly a read which collects the result - which is stored in a
  630. * file-local buffer.
  631. */
  632. void simple_transaction_set(struct file *file, size_t n)
  633. {
  634. struct simple_transaction_argresp *ar = file->private_data;
  635. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  636. /*
  637. * The barrier ensures that ar->size will really remain zero until
  638. * ar->data is ready for reading.
  639. */
  640. smp_mb();
  641. ar->size = n;
  642. }
  643. EXPORT_SYMBOL(simple_transaction_set);
  644. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  645. {
  646. struct simple_transaction_argresp *ar;
  647. static DEFINE_SPINLOCK(simple_transaction_lock);
  648. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  649. return ERR_PTR(-EFBIG);
  650. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  651. if (!ar)
  652. return ERR_PTR(-ENOMEM);
  653. spin_lock(&simple_transaction_lock);
  654. /* only one write allowed per open */
  655. if (file->private_data) {
  656. spin_unlock(&simple_transaction_lock);
  657. free_page((unsigned long)ar);
  658. return ERR_PTR(-EBUSY);
  659. }
  660. file->private_data = ar;
  661. spin_unlock(&simple_transaction_lock);
  662. if (copy_from_user(ar->data, buf, size))
  663. return ERR_PTR(-EFAULT);
  664. return ar->data;
  665. }
  666. EXPORT_SYMBOL(simple_transaction_get);
  667. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  668. {
  669. struct simple_transaction_argresp *ar = file->private_data;
  670. if (!ar)
  671. return 0;
  672. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  673. }
  674. EXPORT_SYMBOL(simple_transaction_read);
  675. int simple_transaction_release(struct inode *inode, struct file *file)
  676. {
  677. free_page((unsigned long)file->private_data);
  678. return 0;
  679. }
  680. EXPORT_SYMBOL(simple_transaction_release);
  681. /* Simple attribute files */
  682. struct simple_attr {
  683. int (*get)(void *, u64 *);
  684. int (*set)(void *, u64);
  685. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  686. char set_buf[24];
  687. void *data;
  688. const char *fmt; /* format for read operation */
  689. struct mutex mutex; /* protects access to these buffers */
  690. };
  691. /* simple_attr_open is called by an actual attribute open file operation
  692. * to set the attribute specific access operations. */
  693. int simple_attr_open(struct inode *inode, struct file *file,
  694. int (*get)(void *, u64 *), int (*set)(void *, u64),
  695. const char *fmt)
  696. {
  697. struct simple_attr *attr;
  698. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  699. if (!attr)
  700. return -ENOMEM;
  701. attr->get = get;
  702. attr->set = set;
  703. attr->data = inode->i_private;
  704. attr->fmt = fmt;
  705. mutex_init(&attr->mutex);
  706. file->private_data = attr;
  707. return nonseekable_open(inode, file);
  708. }
  709. EXPORT_SYMBOL_GPL(simple_attr_open);
  710. int simple_attr_release(struct inode *inode, struct file *file)
  711. {
  712. kfree(file->private_data);
  713. return 0;
  714. }
  715. EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
  716. /* read from the buffer that is filled with the get function */
  717. ssize_t simple_attr_read(struct file *file, char __user *buf,
  718. size_t len, loff_t *ppos)
  719. {
  720. struct simple_attr *attr;
  721. size_t size;
  722. ssize_t ret;
  723. attr = file->private_data;
  724. if (!attr->get)
  725. return -EACCES;
  726. ret = mutex_lock_interruptible(&attr->mutex);
  727. if (ret)
  728. return ret;
  729. if (*ppos) { /* continued read */
  730. size = strlen(attr->get_buf);
  731. } else { /* first read */
  732. u64 val;
  733. ret = attr->get(attr->data, &val);
  734. if (ret)
  735. goto out;
  736. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  737. attr->fmt, (unsigned long long)val);
  738. }
  739. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  740. out:
  741. mutex_unlock(&attr->mutex);
  742. return ret;
  743. }
  744. EXPORT_SYMBOL_GPL(simple_attr_read);
  745. /* interpret the buffer as a number to call the set function with */
  746. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  747. size_t len, loff_t *ppos)
  748. {
  749. struct simple_attr *attr;
  750. u64 val;
  751. size_t size;
  752. ssize_t ret;
  753. attr = file->private_data;
  754. if (!attr->set)
  755. return -EACCES;
  756. ret = mutex_lock_interruptible(&attr->mutex);
  757. if (ret)
  758. return ret;
  759. ret = -EFAULT;
  760. size = min(sizeof(attr->set_buf) - 1, len);
  761. if (copy_from_user(attr->set_buf, buf, size))
  762. goto out;
  763. attr->set_buf[size] = '\0';
  764. val = simple_strtoll(attr->set_buf, NULL, 0);
  765. ret = attr->set(attr->data, val);
  766. if (ret == 0)
  767. ret = len; /* on success, claim we got the whole input */
  768. out:
  769. mutex_unlock(&attr->mutex);
  770. return ret;
  771. }
  772. EXPORT_SYMBOL_GPL(simple_attr_write);
  773. /**
  774. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  775. * @sb: filesystem to do the file handle conversion on
  776. * @fid: file handle to convert
  777. * @fh_len: length of the file handle in bytes
  778. * @fh_type: type of file handle
  779. * @get_inode: filesystem callback to retrieve inode
  780. *
  781. * This function decodes @fid as long as it has one of the well-known
  782. * Linux filehandle types and calls @get_inode on it to retrieve the
  783. * inode for the object specified in the file handle.
  784. */
  785. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  786. int fh_len, int fh_type, struct inode *(*get_inode)
  787. (struct super_block *sb, u64 ino, u32 gen))
  788. {
  789. struct inode *inode = NULL;
  790. if (fh_len < 2)
  791. return NULL;
  792. switch (fh_type) {
  793. case FILEID_INO32_GEN:
  794. case FILEID_INO32_GEN_PARENT:
  795. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  796. break;
  797. }
  798. return d_obtain_alias(inode);
  799. }
  800. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  801. /**
  802. * generic_fh_to_parent - generic helper for the fh_to_parent export operation
  803. * @sb: filesystem to do the file handle conversion on
  804. * @fid: file handle to convert
  805. * @fh_len: length of the file handle in bytes
  806. * @fh_type: type of file handle
  807. * @get_inode: filesystem callback to retrieve inode
  808. *
  809. * This function decodes @fid as long as it has one of the well-known
  810. * Linux filehandle types and calls @get_inode on it to retrieve the
  811. * inode for the _parent_ object specified in the file handle if it
  812. * is specified in the file handle, or NULL otherwise.
  813. */
  814. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  815. int fh_len, int fh_type, struct inode *(*get_inode)
  816. (struct super_block *sb, u64 ino, u32 gen))
  817. {
  818. struct inode *inode = NULL;
  819. if (fh_len <= 2)
  820. return NULL;
  821. switch (fh_type) {
  822. case FILEID_INO32_GEN_PARENT:
  823. inode = get_inode(sb, fid->i32.parent_ino,
  824. (fh_len > 3 ? fid->i32.parent_gen : 0));
  825. break;
  826. }
  827. return d_obtain_alias(inode);
  828. }
  829. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  830. /**
  831. * __generic_file_fsync - generic fsync implementation for simple filesystems
  832. *
  833. * @file: file to synchronize
  834. * @start: start offset in bytes
  835. * @end: end offset in bytes (inclusive)
  836. * @datasync: only synchronize essential metadata if true
  837. *
  838. * This is a generic implementation of the fsync method for simple
  839. * filesystems which track all non-inode metadata in the buffers list
  840. * hanging off the address_space structure.
  841. */
  842. int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
  843. int datasync)
  844. {
  845. struct inode *inode = file->f_mapping->host;
  846. int err;
  847. int ret;
  848. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  849. if (err)
  850. return err;
  851. inode_lock(inode);
  852. ret = sync_mapping_buffers(inode->i_mapping);
  853. if (!(inode->i_state & I_DIRTY_ALL))
  854. goto out;
  855. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  856. goto out;
  857. err = sync_inode_metadata(inode, 1);
  858. if (ret == 0)
  859. ret = err;
  860. out:
  861. inode_unlock(inode);
  862. return ret;
  863. }
  864. EXPORT_SYMBOL(__generic_file_fsync);
  865. /**
  866. * generic_file_fsync - generic fsync implementation for simple filesystems
  867. * with flush
  868. * @file: file to synchronize
  869. * @start: start offset in bytes
  870. * @end: end offset in bytes (inclusive)
  871. * @datasync: only synchronize essential metadata if true
  872. *
  873. */
  874. int generic_file_fsync(struct file *file, loff_t start, loff_t end,
  875. int datasync)
  876. {
  877. struct inode *inode = file->f_mapping->host;
  878. int err;
  879. err = __generic_file_fsync(file, start, end, datasync);
  880. if (err)
  881. return err;
  882. return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  883. }
  884. EXPORT_SYMBOL(generic_file_fsync);
  885. /**
  886. * generic_check_addressable - Check addressability of file system
  887. * @blocksize_bits: log of file system block size
  888. * @num_blocks: number of blocks in file system
  889. *
  890. * Determine whether a file system with @num_blocks blocks (and a
  891. * block size of 2**@blocksize_bits) is addressable by the sector_t
  892. * and page cache of the system. Return 0 if so and -EFBIG otherwise.
  893. */
  894. int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
  895. {
  896. u64 last_fs_block = num_blocks - 1;
  897. u64 last_fs_page =
  898. last_fs_block >> (PAGE_SHIFT - blocksize_bits);
  899. if (unlikely(num_blocks == 0))
  900. return 0;
  901. if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
  902. return -EINVAL;
  903. if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
  904. (last_fs_page > (pgoff_t)(~0ULL))) {
  905. return -EFBIG;
  906. }
  907. return 0;
  908. }
  909. EXPORT_SYMBOL(generic_check_addressable);
  910. /*
  911. * No-op implementation of ->fsync for in-memory filesystems.
  912. */
  913. int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  914. {
  915. return 0;
  916. }
  917. EXPORT_SYMBOL(noop_fsync);
  918. /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
  919. void kfree_link(void *p)
  920. {
  921. kfree(p);
  922. }
  923. EXPORT_SYMBOL(kfree_link);
  924. /*
  925. * nop .set_page_dirty method so that people can use .page_mkwrite on
  926. * anon inodes.
  927. */
  928. static int anon_set_page_dirty(struct page *page)
  929. {
  930. return 0;
  931. };
  932. /*
  933. * A single inode exists for all anon_inode files. Contrary to pipes,
  934. * anon_inode inodes have no associated per-instance data, so we need
  935. * only allocate one of them.
  936. */
  937. struct inode *alloc_anon_inode(struct super_block *s)
  938. {
  939. static const struct address_space_operations anon_aops = {
  940. .set_page_dirty = anon_set_page_dirty,
  941. };
  942. struct inode *inode = new_inode_pseudo(s);
  943. if (!inode)
  944. return ERR_PTR(-ENOMEM);
  945. inode->i_ino = get_next_ino();
  946. inode->i_mapping->a_ops = &anon_aops;
  947. /*
  948. * Mark the inode dirty from the very beginning,
  949. * that way it will never be moved to the dirty
  950. * list because mark_inode_dirty() will think
  951. * that it already _is_ on the dirty list.
  952. */
  953. inode->i_state = I_DIRTY;
  954. inode->i_mode = S_IRUSR | S_IWUSR;
  955. inode->i_uid = current_fsuid();
  956. inode->i_gid = current_fsgid();
  957. inode->i_flags |= S_PRIVATE;
  958. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  959. return inode;
  960. }
  961. EXPORT_SYMBOL(alloc_anon_inode);
  962. /**
  963. * simple_nosetlease - generic helper for prohibiting leases
  964. * @filp: file pointer
  965. * @arg: type of lease to obtain
  966. * @flp: new lease supplied for insertion
  967. * @priv: private data for lm_setup operation
  968. *
  969. * Generic helper for filesystems that do not wish to allow leases to be set.
  970. * All arguments are ignored and it just returns -EINVAL.
  971. */
  972. int
  973. simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
  974. void **priv)
  975. {
  976. return -EINVAL;
  977. }
  978. EXPORT_SYMBOL(simple_nosetlease);
  979. const char *simple_get_link(struct dentry *dentry, struct inode *inode,
  980. struct delayed_call *done)
  981. {
  982. return inode->i_link;
  983. }
  984. EXPORT_SYMBOL(simple_get_link);
  985. const struct inode_operations simple_symlink_inode_operations = {
  986. .get_link = simple_get_link,
  987. .readlink = generic_readlink
  988. };
  989. EXPORT_SYMBOL(simple_symlink_inode_operations);
  990. /*
  991. * Operations for a permanently empty directory.
  992. */
  993. static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  994. {
  995. return ERR_PTR(-ENOENT);
  996. }
  997. static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
  998. struct kstat *stat)
  999. {
  1000. struct inode *inode = d_inode(dentry);
  1001. generic_fillattr(inode, stat);
  1002. return 0;
  1003. }
  1004. static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
  1005. {
  1006. return -EPERM;
  1007. }
  1008. static int empty_dir_setxattr(struct dentry *dentry, struct inode *inode,
  1009. const char *name, const void *value,
  1010. size_t size, int flags)
  1011. {
  1012. return -EOPNOTSUPP;
  1013. }
  1014. static ssize_t empty_dir_getxattr(struct dentry *dentry, struct inode *inode,
  1015. const char *name, void *value, size_t size)
  1016. {
  1017. return -EOPNOTSUPP;
  1018. }
  1019. static int empty_dir_removexattr(struct dentry *dentry, const char *name)
  1020. {
  1021. return -EOPNOTSUPP;
  1022. }
  1023. static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
  1024. {
  1025. return -EOPNOTSUPP;
  1026. }
  1027. static const struct inode_operations empty_dir_inode_operations = {
  1028. .lookup = empty_dir_lookup,
  1029. .permission = generic_permission,
  1030. .setattr = empty_dir_setattr,
  1031. .getattr = empty_dir_getattr,
  1032. .setxattr = empty_dir_setxattr,
  1033. .getxattr = empty_dir_getxattr,
  1034. .removexattr = empty_dir_removexattr,
  1035. .listxattr = empty_dir_listxattr,
  1036. };
  1037. static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
  1038. {
  1039. /* An empty directory has two entries . and .. at offsets 0 and 1 */
  1040. return generic_file_llseek_size(file, offset, whence, 2, 2);
  1041. }
  1042. static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
  1043. {
  1044. dir_emit_dots(file, ctx);
  1045. return 0;
  1046. }
  1047. static const struct file_operations empty_dir_operations = {
  1048. .llseek = empty_dir_llseek,
  1049. .read = generic_read_dir,
  1050. .iterate_shared = empty_dir_readdir,
  1051. .fsync = noop_fsync,
  1052. };
  1053. void make_empty_dir_inode(struct inode *inode)
  1054. {
  1055. set_nlink(inode, 2);
  1056. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  1057. inode->i_uid = GLOBAL_ROOT_UID;
  1058. inode->i_gid = GLOBAL_ROOT_GID;
  1059. inode->i_rdev = 0;
  1060. inode->i_size = 0;
  1061. inode->i_blkbits = PAGE_SHIFT;
  1062. inode->i_blocks = 0;
  1063. inode->i_op = &empty_dir_inode_operations;
  1064. inode->i_fop = &empty_dir_operations;
  1065. }
  1066. bool is_empty_dir_inode(struct inode *inode)
  1067. {
  1068. return (inode->i_fop == &empty_dir_operations) &&
  1069. (inode->i_op == &empty_dir_inode_operations);
  1070. }