libfs.c 27 KB

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