libfs.c 25 KB

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