libfs.c 30 KB

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