libfs.c 22 KB

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