libfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * fs/libfs.c
  3. * Library for filesystems writers.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/slab.h>
  8. #include <linux/mount.h>
  9. #include <linux/vfs.h>
  10. #include <linux/mutex.h>
  11. #include <linux/exportfs.h>
  12. #include <linux/writeback.h>
  13. #include <linux/buffer_head.h>
  14. #include <asm/uaccess.h>
  15. int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
  16. struct kstat *stat)
  17. {
  18. struct inode *inode = dentry->d_inode;
  19. generic_fillattr(inode, stat);
  20. stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
  21. return 0;
  22. }
  23. int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
  24. {
  25. buf->f_type = dentry->d_sb->s_magic;
  26. buf->f_bsize = PAGE_CACHE_SIZE;
  27. buf->f_namelen = NAME_MAX;
  28. return 0;
  29. }
  30. /*
  31. * Retaining negative dentries for an in-memory filesystem just wastes
  32. * memory and lookup time: arrange for them to be deleted immediately.
  33. */
  34. static int simple_delete_dentry(struct dentry *dentry)
  35. {
  36. return 1;
  37. }
  38. /*
  39. * Lookup the data. This is trivial - if the dentry didn't already
  40. * exist, we know it is negative. Set d_op to delete negative dentries.
  41. */
  42. struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  43. {
  44. static const struct dentry_operations simple_dentry_operations = {
  45. .d_delete = simple_delete_dentry,
  46. };
  47. if (dentry->d_name.len > NAME_MAX)
  48. return ERR_PTR(-ENAMETOOLONG);
  49. dentry->d_op = &simple_dentry_operations;
  50. d_add(dentry, NULL);
  51. return NULL;
  52. }
  53. int dcache_dir_open(struct inode *inode, struct file *file)
  54. {
  55. static struct qstr cursor_name = {.len = 1, .name = "."};
  56. file->private_data = d_alloc(file->f_path.dentry, &cursor_name);
  57. return file->private_data ? 0 : -ENOMEM;
  58. }
  59. int dcache_dir_close(struct inode *inode, struct file *file)
  60. {
  61. dput(file->private_data);
  62. return 0;
  63. }
  64. loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
  65. {
  66. mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
  67. switch (origin) {
  68. case 1:
  69. offset += file->f_pos;
  70. case 0:
  71. if (offset >= 0)
  72. break;
  73. default:
  74. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  75. return -EINVAL;
  76. }
  77. if (offset != file->f_pos) {
  78. file->f_pos = offset;
  79. if (file->f_pos >= 2) {
  80. struct list_head *p;
  81. struct dentry *cursor = file->private_data;
  82. loff_t n = file->f_pos - 2;
  83. spin_lock(&dcache_lock);
  84. list_del(&cursor->d_u.d_child);
  85. p = file->f_path.dentry->d_subdirs.next;
  86. while (n && p != &file->f_path.dentry->d_subdirs) {
  87. struct dentry *next;
  88. next = list_entry(p, struct dentry, d_u.d_child);
  89. if (!d_unhashed(next) && next->d_inode)
  90. n--;
  91. p = p->next;
  92. }
  93. list_add_tail(&cursor->d_u.d_child, p);
  94. spin_unlock(&dcache_lock);
  95. }
  96. }
  97. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  98. return offset;
  99. }
  100. /* Relationship between i_mode and the DT_xxx types */
  101. static inline unsigned char dt_type(struct inode *inode)
  102. {
  103. return (inode->i_mode >> 12) & 15;
  104. }
  105. /*
  106. * Directory is locked and all positive dentries in it are safe, since
  107. * for ramfs-type trees they can't go away without unlink() or rmdir(),
  108. * both impossible due to the lock on directory.
  109. */
  110. int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
  111. {
  112. struct dentry *dentry = filp->f_path.dentry;
  113. struct dentry *cursor = filp->private_data;
  114. struct list_head *p, *q = &cursor->d_u.d_child;
  115. ino_t ino;
  116. int i = filp->f_pos;
  117. switch (i) {
  118. case 0:
  119. ino = dentry->d_inode->i_ino;
  120. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  121. break;
  122. filp->f_pos++;
  123. i++;
  124. /* fallthrough */
  125. case 1:
  126. ino = parent_ino(dentry);
  127. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  128. break;
  129. filp->f_pos++;
  130. i++;
  131. /* fallthrough */
  132. default:
  133. spin_lock(&dcache_lock);
  134. if (filp->f_pos == 2)
  135. list_move(q, &dentry->d_subdirs);
  136. for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
  137. struct dentry *next;
  138. next = list_entry(p, struct dentry, d_u.d_child);
  139. if (d_unhashed(next) || !next->d_inode)
  140. continue;
  141. spin_unlock(&dcache_lock);
  142. if (filldir(dirent, next->d_name.name,
  143. next->d_name.len, filp->f_pos,
  144. next->d_inode->i_ino,
  145. dt_type(next->d_inode)) < 0)
  146. return 0;
  147. spin_lock(&dcache_lock);
  148. /* next is still alive */
  149. list_move(q, p);
  150. p = q;
  151. filp->f_pos++;
  152. }
  153. spin_unlock(&dcache_lock);
  154. }
  155. return 0;
  156. }
  157. ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
  158. {
  159. return -EISDIR;
  160. }
  161. const struct file_operations simple_dir_operations = {
  162. .open = dcache_dir_open,
  163. .release = dcache_dir_close,
  164. .llseek = dcache_dir_lseek,
  165. .read = generic_read_dir,
  166. .readdir = dcache_readdir,
  167. .fsync = noop_fsync,
  168. };
  169. const struct inode_operations simple_dir_inode_operations = {
  170. .lookup = simple_lookup,
  171. };
  172. static const struct super_operations simple_super_operations = {
  173. .statfs = simple_statfs,
  174. };
  175. /*
  176. * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
  177. * will never be mountable)
  178. */
  179. int get_sb_pseudo(struct file_system_type *fs_type, char *name,
  180. const struct super_operations *ops, unsigned long magic,
  181. struct vfsmount *mnt)
  182. {
  183. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  184. struct dentry *dentry;
  185. struct inode *root;
  186. struct qstr d_name = {.name = name, .len = strlen(name)};
  187. if (IS_ERR(s))
  188. return PTR_ERR(s);
  189. s->s_flags = MS_NOUSER;
  190. s->s_maxbytes = MAX_LFS_FILESIZE;
  191. s->s_blocksize = PAGE_SIZE;
  192. s->s_blocksize_bits = PAGE_SHIFT;
  193. s->s_magic = magic;
  194. s->s_op = ops ? ops : &simple_super_operations;
  195. s->s_time_gran = 1;
  196. root = new_inode(s);
  197. if (!root)
  198. goto Enomem;
  199. /*
  200. * since this is the first inode, make it number 1. New inodes created
  201. * after this must take care not to collide with it (by passing
  202. * max_reserved of 1 to iunique).
  203. */
  204. root->i_ino = 1;
  205. root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
  206. root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
  207. dentry = d_alloc(NULL, &d_name);
  208. if (!dentry) {
  209. iput(root);
  210. goto Enomem;
  211. }
  212. dentry->d_sb = s;
  213. dentry->d_parent = dentry;
  214. d_instantiate(dentry, root);
  215. s->s_root = dentry;
  216. s->s_flags |= MS_ACTIVE;
  217. simple_set_mnt(mnt, s);
  218. return 0;
  219. Enomem:
  220. deactivate_locked_super(s);
  221. return -ENOMEM;
  222. }
  223. int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  224. {
  225. struct inode *inode = old_dentry->d_inode;
  226. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  227. inc_nlink(inode);
  228. atomic_inc(&inode->i_count);
  229. dget(dentry);
  230. d_instantiate(dentry, inode);
  231. return 0;
  232. }
  233. static inline int simple_positive(struct dentry *dentry)
  234. {
  235. return dentry->d_inode && !d_unhashed(dentry);
  236. }
  237. int simple_empty(struct dentry *dentry)
  238. {
  239. struct dentry *child;
  240. int ret = 0;
  241. spin_lock(&dcache_lock);
  242. list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child)
  243. if (simple_positive(child))
  244. goto out;
  245. ret = 1;
  246. out:
  247. spin_unlock(&dcache_lock);
  248. return ret;
  249. }
  250. int simple_unlink(struct inode *dir, struct dentry *dentry)
  251. {
  252. struct inode *inode = dentry->d_inode;
  253. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  254. drop_nlink(inode);
  255. dput(dentry);
  256. return 0;
  257. }
  258. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  259. {
  260. if (!simple_empty(dentry))
  261. return -ENOTEMPTY;
  262. drop_nlink(dentry->d_inode);
  263. simple_unlink(dir, dentry);
  264. drop_nlink(dir);
  265. return 0;
  266. }
  267. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  268. struct inode *new_dir, struct dentry *new_dentry)
  269. {
  270. struct inode *inode = old_dentry->d_inode;
  271. int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
  272. if (!simple_empty(new_dentry))
  273. return -ENOTEMPTY;
  274. if (new_dentry->d_inode) {
  275. simple_unlink(new_dir, new_dentry);
  276. if (they_are_dirs)
  277. drop_nlink(old_dir);
  278. } else if (they_are_dirs) {
  279. drop_nlink(old_dir);
  280. inc_nlink(new_dir);
  281. }
  282. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  283. new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
  284. return 0;
  285. }
  286. int simple_readpage(struct file *file, struct page *page)
  287. {
  288. clear_highpage(page);
  289. flush_dcache_page(page);
  290. SetPageUptodate(page);
  291. unlock_page(page);
  292. return 0;
  293. }
  294. int simple_write_begin(struct file *file, struct address_space *mapping,
  295. loff_t pos, unsigned len, unsigned flags,
  296. struct page **pagep, void **fsdata)
  297. {
  298. struct page *page;
  299. pgoff_t index;
  300. index = pos >> PAGE_CACHE_SHIFT;
  301. page = grab_cache_page_write_begin(mapping, index, flags);
  302. if (!page)
  303. return -ENOMEM;
  304. *pagep = page;
  305. if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
  306. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  307. zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE);
  308. }
  309. return 0;
  310. }
  311. /**
  312. * simple_write_end - .write_end helper for non-block-device FSes
  313. * @available: See .write_end of address_space_operations
  314. * @file: "
  315. * @mapping: "
  316. * @pos: "
  317. * @len: "
  318. * @copied: "
  319. * @page: "
  320. * @fsdata: "
  321. *
  322. * simple_write_end does the minimum needed for updating a page after writing is
  323. * done. It has the same API signature as the .write_end of
  324. * address_space_operations vector. So it can just be set onto .write_end for
  325. * FSes that don't need any other processing. i_mutex is assumed to be held.
  326. * Block based filesystems should use generic_write_end().
  327. * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
  328. * is not called, so a filesystem that actually does store data in .write_inode
  329. * should extend on what's done here with a call to mark_inode_dirty() in the
  330. * case that i_size has changed.
  331. */
  332. int simple_write_end(struct file *file, struct address_space *mapping,
  333. loff_t pos, unsigned len, unsigned copied,
  334. struct page *page, void *fsdata)
  335. {
  336. struct inode *inode = page->mapping->host;
  337. loff_t last_pos = pos + copied;
  338. /* zero the stale part of the page if we did a short copy */
  339. if (copied < len) {
  340. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  341. zero_user(page, from + copied, len - copied);
  342. }
  343. if (!PageUptodate(page))
  344. SetPageUptodate(page);
  345. /*
  346. * No need to use i_size_read() here, the i_size
  347. * cannot change under us because we hold the i_mutex.
  348. */
  349. if (last_pos > inode->i_size)
  350. i_size_write(inode, last_pos);
  351. set_page_dirty(page);
  352. unlock_page(page);
  353. page_cache_release(page);
  354. return copied;
  355. }
  356. /*
  357. * the inodes created here are not hashed. If you use iunique to generate
  358. * unique inode values later for this filesystem, then you must take care
  359. * to pass it an appropriate max_reserved value to avoid collisions.
  360. */
  361. int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
  362. {
  363. struct inode *inode;
  364. struct dentry *root;
  365. struct dentry *dentry;
  366. int i;
  367. s->s_blocksize = PAGE_CACHE_SIZE;
  368. s->s_blocksize_bits = PAGE_CACHE_SHIFT;
  369. s->s_magic = magic;
  370. s->s_op = &simple_super_operations;
  371. s->s_time_gran = 1;
  372. inode = new_inode(s);
  373. if (!inode)
  374. return -ENOMEM;
  375. /*
  376. * because the root inode is 1, the files array must not contain an
  377. * entry at index 1
  378. */
  379. inode->i_ino = 1;
  380. inode->i_mode = S_IFDIR | 0755;
  381. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  382. inode->i_op = &simple_dir_inode_operations;
  383. inode->i_fop = &simple_dir_operations;
  384. inode->i_nlink = 2;
  385. root = d_alloc_root(inode);
  386. if (!root) {
  387. iput(inode);
  388. return -ENOMEM;
  389. }
  390. for (i = 0; !files->name || files->name[0]; i++, files++) {
  391. if (!files->name)
  392. continue;
  393. /* warn if it tries to conflict with the root inode */
  394. if (unlikely(i == 1))
  395. printk(KERN_WARNING "%s: %s passed in a files array"
  396. "with an index of 1!\n", __func__,
  397. s->s_type->name);
  398. dentry = d_alloc_name(root, files->name);
  399. if (!dentry)
  400. goto out;
  401. inode = new_inode(s);
  402. if (!inode)
  403. goto out;
  404. inode->i_mode = S_IFREG | files->mode;
  405. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  406. inode->i_fop = files->ops;
  407. inode->i_ino = i;
  408. d_add(dentry, inode);
  409. }
  410. s->s_root = root;
  411. return 0;
  412. out:
  413. d_genocide(root);
  414. dput(root);
  415. return -ENOMEM;
  416. }
  417. static DEFINE_SPINLOCK(pin_fs_lock);
  418. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  419. {
  420. struct vfsmount *mnt = NULL;
  421. spin_lock(&pin_fs_lock);
  422. if (unlikely(!*mount)) {
  423. spin_unlock(&pin_fs_lock);
  424. mnt = vfs_kern_mount(type, 0, type->name, NULL);
  425. if (IS_ERR(mnt))
  426. return PTR_ERR(mnt);
  427. spin_lock(&pin_fs_lock);
  428. if (!*mount)
  429. *mount = mnt;
  430. }
  431. mntget(*mount);
  432. ++*count;
  433. spin_unlock(&pin_fs_lock);
  434. mntput(mnt);
  435. return 0;
  436. }
  437. void simple_release_fs(struct vfsmount **mount, int *count)
  438. {
  439. struct vfsmount *mnt;
  440. spin_lock(&pin_fs_lock);
  441. mnt = *mount;
  442. if (!--*count)
  443. *mount = NULL;
  444. spin_unlock(&pin_fs_lock);
  445. mntput(mnt);
  446. }
  447. /**
  448. * simple_read_from_buffer - copy data from the buffer to user space
  449. * @to: the user space buffer to read to
  450. * @count: the maximum number of bytes to read
  451. * @ppos: the current position in the buffer
  452. * @from: the buffer to read from
  453. * @available: the size of the buffer
  454. *
  455. * The simple_read_from_buffer() function reads up to @count bytes from the
  456. * buffer @from at offset @ppos into the user space address starting at @to.
  457. *
  458. * On success, the number of bytes read is returned and the offset @ppos is
  459. * advanced by this number, or negative value is returned on error.
  460. **/
  461. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  462. const void *from, size_t available)
  463. {
  464. loff_t pos = *ppos;
  465. size_t ret;
  466. if (pos < 0)
  467. return -EINVAL;
  468. if (pos >= available || !count)
  469. return 0;
  470. if (count > available - pos)
  471. count = available - pos;
  472. ret = copy_to_user(to, from + pos, count);
  473. if (ret == count)
  474. return -EFAULT;
  475. count -= ret;
  476. *ppos = pos + count;
  477. return count;
  478. }
  479. /**
  480. * simple_write_to_buffer - copy data from user space to the buffer
  481. * @to: the buffer to write to
  482. * @available: the size of the buffer
  483. * @ppos: the current position in the buffer
  484. * @from: the user space buffer to read from
  485. * @count: the maximum number of bytes to read
  486. *
  487. * The simple_write_to_buffer() function reads up to @count bytes from the user
  488. * space address starting at @from into the buffer @to at offset @ppos.
  489. *
  490. * On success, the number of bytes written is returned and the offset @ppos is
  491. * advanced by this number, or negative value is returned on error.
  492. **/
  493. ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  494. const void __user *from, size_t count)
  495. {
  496. loff_t pos = *ppos;
  497. size_t res;
  498. if (pos < 0)
  499. return -EINVAL;
  500. if (pos >= available || !count)
  501. return 0;
  502. if (count > available - pos)
  503. count = available - pos;
  504. res = copy_from_user(to + pos, from, count);
  505. if (res == count)
  506. return -EFAULT;
  507. count -= res;
  508. *ppos = pos + count;
  509. return count;
  510. }
  511. /**
  512. * memory_read_from_buffer - copy data from the buffer
  513. * @to: the kernel space buffer to read to
  514. * @count: the maximum number of bytes to read
  515. * @ppos: the current position in the buffer
  516. * @from: the buffer to read from
  517. * @available: the size of the buffer
  518. *
  519. * The memory_read_from_buffer() function reads up to @count bytes from the
  520. * buffer @from at offset @ppos into the kernel space address starting at @to.
  521. *
  522. * On success, the number of bytes read is returned and the offset @ppos is
  523. * advanced by this number, or negative value is returned on error.
  524. **/
  525. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  526. const void *from, size_t available)
  527. {
  528. loff_t pos = *ppos;
  529. if (pos < 0)
  530. return -EINVAL;
  531. if (pos >= available)
  532. return 0;
  533. if (count > available - pos)
  534. count = available - pos;
  535. memcpy(to, from + pos, count);
  536. *ppos = pos + count;
  537. return count;
  538. }
  539. /*
  540. * Transaction based IO.
  541. * The file expects a single write which triggers the transaction, and then
  542. * possibly a read which collects the result - which is stored in a
  543. * file-local buffer.
  544. */
  545. void simple_transaction_set(struct file *file, size_t n)
  546. {
  547. struct simple_transaction_argresp *ar = file->private_data;
  548. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  549. /*
  550. * The barrier ensures that ar->size will really remain zero until
  551. * ar->data is ready for reading.
  552. */
  553. smp_mb();
  554. ar->size = n;
  555. }
  556. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  557. {
  558. struct simple_transaction_argresp *ar;
  559. static DEFINE_SPINLOCK(simple_transaction_lock);
  560. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  561. return ERR_PTR(-EFBIG);
  562. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  563. if (!ar)
  564. return ERR_PTR(-ENOMEM);
  565. spin_lock(&simple_transaction_lock);
  566. /* only one write allowed per open */
  567. if (file->private_data) {
  568. spin_unlock(&simple_transaction_lock);
  569. free_page((unsigned long)ar);
  570. return ERR_PTR(-EBUSY);
  571. }
  572. file->private_data = ar;
  573. spin_unlock(&simple_transaction_lock);
  574. if (copy_from_user(ar->data, buf, size))
  575. return ERR_PTR(-EFAULT);
  576. return ar->data;
  577. }
  578. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  579. {
  580. struct simple_transaction_argresp *ar = file->private_data;
  581. if (!ar)
  582. return 0;
  583. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  584. }
  585. int simple_transaction_release(struct inode *inode, struct file *file)
  586. {
  587. free_page((unsigned long)file->private_data);
  588. return 0;
  589. }
  590. /* Simple attribute files */
  591. struct simple_attr {
  592. int (*get)(void *, u64 *);
  593. int (*set)(void *, u64);
  594. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  595. char set_buf[24];
  596. void *data;
  597. const char *fmt; /* format for read operation */
  598. struct mutex mutex; /* protects access to these buffers */
  599. };
  600. /* simple_attr_open is called by an actual attribute open file operation
  601. * to set the attribute specific access operations. */
  602. int simple_attr_open(struct inode *inode, struct file *file,
  603. int (*get)(void *, u64 *), int (*set)(void *, u64),
  604. const char *fmt)
  605. {
  606. struct simple_attr *attr;
  607. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  608. if (!attr)
  609. return -ENOMEM;
  610. attr->get = get;
  611. attr->set = set;
  612. attr->data = inode->i_private;
  613. attr->fmt = fmt;
  614. mutex_init(&attr->mutex);
  615. file->private_data = attr;
  616. return nonseekable_open(inode, file);
  617. }
  618. int simple_attr_release(struct inode *inode, struct file *file)
  619. {
  620. kfree(file->private_data);
  621. return 0;
  622. }
  623. /* read from the buffer that is filled with the get function */
  624. ssize_t simple_attr_read(struct file *file, char __user *buf,
  625. size_t len, loff_t *ppos)
  626. {
  627. struct simple_attr *attr;
  628. size_t size;
  629. ssize_t ret;
  630. attr = file->private_data;
  631. if (!attr->get)
  632. return -EACCES;
  633. ret = mutex_lock_interruptible(&attr->mutex);
  634. if (ret)
  635. return ret;
  636. if (*ppos) { /* continued read */
  637. size = strlen(attr->get_buf);
  638. } else { /* first read */
  639. u64 val;
  640. ret = attr->get(attr->data, &val);
  641. if (ret)
  642. goto out;
  643. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  644. attr->fmt, (unsigned long long)val);
  645. }
  646. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  647. out:
  648. mutex_unlock(&attr->mutex);
  649. return ret;
  650. }
  651. /* interpret the buffer as a number to call the set function with */
  652. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  653. size_t len, loff_t *ppos)
  654. {
  655. struct simple_attr *attr;
  656. u64 val;
  657. size_t size;
  658. ssize_t ret;
  659. attr = file->private_data;
  660. if (!attr->set)
  661. return -EACCES;
  662. ret = mutex_lock_interruptible(&attr->mutex);
  663. if (ret)
  664. return ret;
  665. ret = -EFAULT;
  666. size = min(sizeof(attr->set_buf) - 1, len);
  667. if (copy_from_user(attr->set_buf, buf, size))
  668. goto out;
  669. attr->set_buf[size] = '\0';
  670. val = simple_strtol(attr->set_buf, NULL, 0);
  671. ret = attr->set(attr->data, val);
  672. if (ret == 0)
  673. ret = len; /* on success, claim we got the whole input */
  674. out:
  675. mutex_unlock(&attr->mutex);
  676. return ret;
  677. }
  678. /**
  679. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  680. * @sb: filesystem to do the file handle conversion on
  681. * @fid: file handle to convert
  682. * @fh_len: length of the file handle in bytes
  683. * @fh_type: type of file handle
  684. * @get_inode: filesystem callback to retrieve inode
  685. *
  686. * This function decodes @fid as long as it has one of the well-known
  687. * Linux filehandle types and calls @get_inode on it to retrieve the
  688. * inode for the object specified in the file handle.
  689. */
  690. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  691. int fh_len, int fh_type, struct inode *(*get_inode)
  692. (struct super_block *sb, u64 ino, u32 gen))
  693. {
  694. struct inode *inode = NULL;
  695. if (fh_len < 2)
  696. return NULL;
  697. switch (fh_type) {
  698. case FILEID_INO32_GEN:
  699. case FILEID_INO32_GEN_PARENT:
  700. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  701. break;
  702. }
  703. return d_obtain_alias(inode);
  704. }
  705. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  706. /**
  707. * generic_fh_to_dentry - generic helper for the fh_to_parent export operation
  708. * @sb: filesystem to do the file handle conversion on
  709. * @fid: file handle to convert
  710. * @fh_len: length of the file handle in bytes
  711. * @fh_type: type of file handle
  712. * @get_inode: filesystem callback to retrieve inode
  713. *
  714. * This function decodes @fid as long as it has one of the well-known
  715. * Linux filehandle types and calls @get_inode on it to retrieve the
  716. * inode for the _parent_ object specified in the file handle if it
  717. * is specified in the file handle, or NULL otherwise.
  718. */
  719. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  720. int fh_len, int fh_type, struct inode *(*get_inode)
  721. (struct super_block *sb, u64 ino, u32 gen))
  722. {
  723. struct inode *inode = NULL;
  724. if (fh_len <= 2)
  725. return NULL;
  726. switch (fh_type) {
  727. case FILEID_INO32_GEN_PARENT:
  728. inode = get_inode(sb, fid->i32.parent_ino,
  729. (fh_len > 3 ? fid->i32.parent_gen : 0));
  730. break;
  731. }
  732. return d_obtain_alias(inode);
  733. }
  734. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  735. /**
  736. * generic_file_fsync - generic fsync implementation for simple filesystems
  737. * @file: file to synchronize
  738. * @datasync: only synchronize essential metadata if true
  739. *
  740. * This is a generic implementation of the fsync method for simple
  741. * filesystems which track all non-inode metadata in the buffers list
  742. * hanging off the address_space structure.
  743. */
  744. int generic_file_fsync(struct file *file, int datasync)
  745. {
  746. struct writeback_control wbc = {
  747. .sync_mode = WB_SYNC_ALL,
  748. .nr_to_write = 0, /* metadata-only; caller takes care of data */
  749. };
  750. struct inode *inode = file->f_mapping->host;
  751. int err;
  752. int ret;
  753. ret = sync_mapping_buffers(inode->i_mapping);
  754. if (!(inode->i_state & I_DIRTY))
  755. return ret;
  756. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  757. return ret;
  758. err = sync_inode(inode, &wbc);
  759. if (ret == 0)
  760. ret = err;
  761. return ret;
  762. }
  763. EXPORT_SYMBOL(generic_file_fsync);
  764. /*
  765. * No-op implementation of ->fsync for in-memory filesystems.
  766. */
  767. int noop_fsync(struct file *file, int datasync)
  768. {
  769. return 0;
  770. }
  771. EXPORT_SYMBOL(dcache_dir_close);
  772. EXPORT_SYMBOL(dcache_dir_lseek);
  773. EXPORT_SYMBOL(dcache_dir_open);
  774. EXPORT_SYMBOL(dcache_readdir);
  775. EXPORT_SYMBOL(generic_read_dir);
  776. EXPORT_SYMBOL(get_sb_pseudo);
  777. EXPORT_SYMBOL(simple_write_begin);
  778. EXPORT_SYMBOL(simple_write_end);
  779. EXPORT_SYMBOL(simple_dir_inode_operations);
  780. EXPORT_SYMBOL(simple_dir_operations);
  781. EXPORT_SYMBOL(simple_empty);
  782. EXPORT_SYMBOL(simple_fill_super);
  783. EXPORT_SYMBOL(simple_getattr);
  784. EXPORT_SYMBOL(simple_link);
  785. EXPORT_SYMBOL(simple_lookup);
  786. EXPORT_SYMBOL(simple_pin_fs);
  787. EXPORT_SYMBOL(simple_readpage);
  788. EXPORT_SYMBOL(simple_release_fs);
  789. EXPORT_SYMBOL(simple_rename);
  790. EXPORT_SYMBOL(simple_rmdir);
  791. EXPORT_SYMBOL(simple_statfs);
  792. EXPORT_SYMBOL(noop_fsync);
  793. EXPORT_SYMBOL(simple_unlink);
  794. EXPORT_SYMBOL(simple_read_from_buffer);
  795. EXPORT_SYMBOL(simple_write_to_buffer);
  796. EXPORT_SYMBOL(memory_read_from_buffer);
  797. EXPORT_SYMBOL(simple_transaction_set);
  798. EXPORT_SYMBOL(simple_transaction_get);
  799. EXPORT_SYMBOL(simple_transaction_read);
  800. EXPORT_SYMBOL(simple_transaction_release);
  801. EXPORT_SYMBOL_GPL(simple_attr_open);
  802. EXPORT_SYMBOL_GPL(simple_attr_release);
  803. EXPORT_SYMBOL_GPL(simple_attr_read);
  804. EXPORT_SYMBOL_GPL(simple_attr_write);