libfs.c 30 KB

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