libfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. * memory_read_from_buffer - copy data from the buffer
  485. * @to: the kernel space buffer to read to
  486. * @count: the maximum number of bytes to read
  487. * @ppos: the current position in the buffer
  488. * @from: the buffer to read from
  489. * @available: the size of the buffer
  490. *
  491. * The memory_read_from_buffer() function reads up to @count bytes from the
  492. * buffer @from at offset @ppos into the kernel space address starting at @to.
  493. *
  494. * On success, the number of bytes read is returned and the offset @ppos is
  495. * advanced by this number, or negative value is returned on error.
  496. **/
  497. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  498. const void *from, size_t available)
  499. {
  500. loff_t pos = *ppos;
  501. if (pos < 0)
  502. return -EINVAL;
  503. if (pos >= available)
  504. return 0;
  505. if (count > available - pos)
  506. count = available - pos;
  507. memcpy(to, from + pos, count);
  508. *ppos = pos + count;
  509. return count;
  510. }
  511. /*
  512. * Transaction based IO.
  513. * The file expects a single write which triggers the transaction, and then
  514. * possibly a read which collects the result - which is stored in a
  515. * file-local buffer.
  516. */
  517. void simple_transaction_set(struct file *file, size_t n)
  518. {
  519. struct simple_transaction_argresp *ar = file->private_data;
  520. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  521. /*
  522. * The barrier ensures that ar->size will really remain zero until
  523. * ar->data is ready for reading.
  524. */
  525. smp_mb();
  526. ar->size = n;
  527. }
  528. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  529. {
  530. struct simple_transaction_argresp *ar;
  531. static DEFINE_SPINLOCK(simple_transaction_lock);
  532. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  533. return ERR_PTR(-EFBIG);
  534. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  535. if (!ar)
  536. return ERR_PTR(-ENOMEM);
  537. spin_lock(&simple_transaction_lock);
  538. /* only one write allowed per open */
  539. if (file->private_data) {
  540. spin_unlock(&simple_transaction_lock);
  541. free_page((unsigned long)ar);
  542. return ERR_PTR(-EBUSY);
  543. }
  544. file->private_data = ar;
  545. spin_unlock(&simple_transaction_lock);
  546. if (copy_from_user(ar->data, buf, size))
  547. return ERR_PTR(-EFAULT);
  548. return ar->data;
  549. }
  550. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  551. {
  552. struct simple_transaction_argresp *ar = file->private_data;
  553. if (!ar)
  554. return 0;
  555. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  556. }
  557. int simple_transaction_release(struct inode *inode, struct file *file)
  558. {
  559. free_page((unsigned long)file->private_data);
  560. return 0;
  561. }
  562. /* Simple attribute files */
  563. struct simple_attr {
  564. int (*get)(void *, u64 *);
  565. int (*set)(void *, u64);
  566. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  567. char set_buf[24];
  568. void *data;
  569. const char *fmt; /* format for read operation */
  570. struct mutex mutex; /* protects access to these buffers */
  571. };
  572. /* simple_attr_open is called by an actual attribute open file operation
  573. * to set the attribute specific access operations. */
  574. int simple_attr_open(struct inode *inode, struct file *file,
  575. int (*get)(void *, u64 *), int (*set)(void *, u64),
  576. const char *fmt)
  577. {
  578. struct simple_attr *attr;
  579. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  580. if (!attr)
  581. return -ENOMEM;
  582. attr->get = get;
  583. attr->set = set;
  584. attr->data = inode->i_private;
  585. attr->fmt = fmt;
  586. mutex_init(&attr->mutex);
  587. file->private_data = attr;
  588. return nonseekable_open(inode, file);
  589. }
  590. int simple_attr_release(struct inode *inode, struct file *file)
  591. {
  592. kfree(file->private_data);
  593. return 0;
  594. }
  595. /* read from the buffer that is filled with the get function */
  596. ssize_t simple_attr_read(struct file *file, char __user *buf,
  597. size_t len, loff_t *ppos)
  598. {
  599. struct simple_attr *attr;
  600. size_t size;
  601. ssize_t ret;
  602. attr = file->private_data;
  603. if (!attr->get)
  604. return -EACCES;
  605. ret = mutex_lock_interruptible(&attr->mutex);
  606. if (ret)
  607. return ret;
  608. if (*ppos) { /* continued read */
  609. size = strlen(attr->get_buf);
  610. } else { /* first read */
  611. u64 val;
  612. ret = attr->get(attr->data, &val);
  613. if (ret)
  614. goto out;
  615. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  616. attr->fmt, (unsigned long long)val);
  617. }
  618. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  619. out:
  620. mutex_unlock(&attr->mutex);
  621. return ret;
  622. }
  623. /* interpret the buffer as a number to call the set function with */
  624. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  625. size_t len, loff_t *ppos)
  626. {
  627. struct simple_attr *attr;
  628. u64 val;
  629. size_t size;
  630. ssize_t ret;
  631. attr = file->private_data;
  632. if (!attr->set)
  633. return -EACCES;
  634. ret = mutex_lock_interruptible(&attr->mutex);
  635. if (ret)
  636. return ret;
  637. ret = -EFAULT;
  638. size = min(sizeof(attr->set_buf) - 1, len);
  639. if (copy_from_user(attr->set_buf, buf, size))
  640. goto out;
  641. attr->set_buf[size] = '\0';
  642. val = simple_strtol(attr->set_buf, NULL, 0);
  643. ret = attr->set(attr->data, val);
  644. if (ret == 0)
  645. ret = len; /* on success, claim we got the whole input */
  646. out:
  647. mutex_unlock(&attr->mutex);
  648. return ret;
  649. }
  650. /**
  651. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  652. * @sb: filesystem to do the file handle conversion on
  653. * @fid: file handle to convert
  654. * @fh_len: length of the file handle in bytes
  655. * @fh_type: type of file handle
  656. * @get_inode: filesystem callback to retrieve inode
  657. *
  658. * This function decodes @fid as long as it has one of the well-known
  659. * Linux filehandle types and calls @get_inode on it to retrieve the
  660. * inode for the object specified in the file handle.
  661. */
  662. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  663. int fh_len, int fh_type, struct inode *(*get_inode)
  664. (struct super_block *sb, u64 ino, u32 gen))
  665. {
  666. struct inode *inode = NULL;
  667. if (fh_len < 2)
  668. return NULL;
  669. switch (fh_type) {
  670. case FILEID_INO32_GEN:
  671. case FILEID_INO32_GEN_PARENT:
  672. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  673. break;
  674. }
  675. return d_obtain_alias(inode);
  676. }
  677. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  678. /**
  679. * generic_fh_to_dentry - generic helper for the fh_to_parent 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 _parent_ object specified in the file handle if it
  689. * is specified in the file handle, or NULL otherwise.
  690. */
  691. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  692. int fh_len, int fh_type, struct inode *(*get_inode)
  693. (struct super_block *sb, u64 ino, u32 gen))
  694. {
  695. struct inode *inode = NULL;
  696. if (fh_len <= 2)
  697. return NULL;
  698. switch (fh_type) {
  699. case FILEID_INO32_GEN_PARENT:
  700. inode = get_inode(sb, fid->i32.parent_ino,
  701. (fh_len > 3 ? fid->i32.parent_gen : 0));
  702. break;
  703. }
  704. return d_obtain_alias(inode);
  705. }
  706. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  707. int simple_fsync(struct file *file, struct dentry *dentry, int datasync)
  708. {
  709. struct writeback_control wbc = {
  710. .sync_mode = WB_SYNC_ALL,
  711. .nr_to_write = 0, /* metadata-only; caller takes care of data */
  712. };
  713. struct inode *inode = dentry->d_inode;
  714. int err;
  715. int ret;
  716. ret = sync_mapping_buffers(inode->i_mapping);
  717. if (!(inode->i_state & I_DIRTY))
  718. return ret;
  719. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  720. return ret;
  721. err = sync_inode(inode, &wbc);
  722. if (ret == 0)
  723. ret = err;
  724. return ret;
  725. }
  726. EXPORT_SYMBOL(simple_fsync);
  727. EXPORT_SYMBOL(dcache_dir_close);
  728. EXPORT_SYMBOL(dcache_dir_lseek);
  729. EXPORT_SYMBOL(dcache_dir_open);
  730. EXPORT_SYMBOL(dcache_readdir);
  731. EXPORT_SYMBOL(generic_read_dir);
  732. EXPORT_SYMBOL(get_sb_pseudo);
  733. EXPORT_SYMBOL(simple_write_begin);
  734. EXPORT_SYMBOL(simple_write_end);
  735. EXPORT_SYMBOL(simple_dir_inode_operations);
  736. EXPORT_SYMBOL(simple_dir_operations);
  737. EXPORT_SYMBOL(simple_empty);
  738. EXPORT_SYMBOL(simple_fill_super);
  739. EXPORT_SYMBOL(simple_getattr);
  740. EXPORT_SYMBOL(simple_link);
  741. EXPORT_SYMBOL(simple_lookup);
  742. EXPORT_SYMBOL(simple_pin_fs);
  743. EXPORT_SYMBOL(simple_readpage);
  744. EXPORT_SYMBOL(simple_release_fs);
  745. EXPORT_SYMBOL(simple_rename);
  746. EXPORT_SYMBOL(simple_rmdir);
  747. EXPORT_SYMBOL(simple_statfs);
  748. EXPORT_SYMBOL(simple_sync_file);
  749. EXPORT_SYMBOL(simple_unlink);
  750. EXPORT_SYMBOL(simple_read_from_buffer);
  751. EXPORT_SYMBOL(memory_read_from_buffer);
  752. EXPORT_SYMBOL(simple_transaction_set);
  753. EXPORT_SYMBOL(simple_transaction_get);
  754. EXPORT_SYMBOL(simple_transaction_read);
  755. EXPORT_SYMBOL(simple_transaction_release);
  756. EXPORT_SYMBOL_GPL(simple_attr_open);
  757. EXPORT_SYMBOL_GPL(simple_attr_release);
  758. EXPORT_SYMBOL_GPL(simple_attr_read);
  759. EXPORT_SYMBOL_GPL(simple_attr_write);