libfs.c 21 KB

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