libfs.c 28 KB

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