libfs.c 25 KB

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