libfs.c 25 KB

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