PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/libfs.c

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