libfs.c 25 KB

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