libfs.c 23 KB

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