libfs.c 25 KB

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