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 <linux/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. * Use *ONLY* with simple_readpage()
  416. */
  417. int simple_write_end(struct file *file, struct address_space *mapping,
  418. loff_t pos, unsigned len, unsigned copied,
  419. struct page *page, void *fsdata)
  420. {
  421. struct inode *inode = page->mapping->host;
  422. loff_t last_pos = pos + copied;
  423. /* zero the stale part of the page if we did a short copy */
  424. if (!PageUptodate(page)) {
  425. if (copied < len) {
  426. unsigned from = pos & (PAGE_SIZE - 1);
  427. zero_user(page, from + copied, len - copied);
  428. }
  429. SetPageUptodate(page);
  430. }
  431. /*
  432. * No need to use i_size_read() here, the i_size
  433. * cannot change under us because we hold the i_mutex.
  434. */
  435. if (last_pos > inode->i_size)
  436. i_size_write(inode, last_pos);
  437. set_page_dirty(page);
  438. unlock_page(page);
  439. put_page(page);
  440. return copied;
  441. }
  442. EXPORT_SYMBOL(simple_write_end);
  443. /*
  444. * the inodes created here are not hashed. If you use iunique to generate
  445. * unique inode values later for this filesystem, then you must take care
  446. * to pass it an appropriate max_reserved value to avoid collisions.
  447. */
  448. int simple_fill_super(struct super_block *s, unsigned long magic,
  449. struct tree_descr *files)
  450. {
  451. struct inode *inode;
  452. struct dentry *root;
  453. struct dentry *dentry;
  454. int i;
  455. s->s_blocksize = PAGE_SIZE;
  456. s->s_blocksize_bits = PAGE_SHIFT;
  457. s->s_magic = magic;
  458. s->s_op = &simple_super_operations;
  459. s->s_time_gran = 1;
  460. inode = new_inode(s);
  461. if (!inode)
  462. return -ENOMEM;
  463. /*
  464. * because the root inode is 1, the files array must not contain an
  465. * entry at index 1
  466. */
  467. inode->i_ino = 1;
  468. inode->i_mode = S_IFDIR | 0755;
  469. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  470. inode->i_op = &simple_dir_inode_operations;
  471. inode->i_fop = &simple_dir_operations;
  472. set_nlink(inode, 2);
  473. root = d_make_root(inode);
  474. if (!root)
  475. return -ENOMEM;
  476. for (i = 0; !files->name || files->name[0]; i++, files++) {
  477. if (!files->name)
  478. continue;
  479. /* warn if it tries to conflict with the root inode */
  480. if (unlikely(i == 1))
  481. printk(KERN_WARNING "%s: %s passed in a files array"
  482. "with an index of 1!\n", __func__,
  483. s->s_type->name);
  484. dentry = d_alloc_name(root, files->name);
  485. if (!dentry)
  486. goto out;
  487. inode = new_inode(s);
  488. if (!inode) {
  489. dput(dentry);
  490. goto out;
  491. }
  492. inode->i_mode = S_IFREG | files->mode;
  493. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  494. inode->i_fop = files->ops;
  495. inode->i_ino = i;
  496. d_add(dentry, inode);
  497. }
  498. s->s_root = root;
  499. return 0;
  500. out:
  501. d_genocide(root);
  502. shrink_dcache_parent(root);
  503. dput(root);
  504. return -ENOMEM;
  505. }
  506. EXPORT_SYMBOL(simple_fill_super);
  507. static DEFINE_SPINLOCK(pin_fs_lock);
  508. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  509. {
  510. struct vfsmount *mnt = NULL;
  511. spin_lock(&pin_fs_lock);
  512. if (unlikely(!*mount)) {
  513. spin_unlock(&pin_fs_lock);
  514. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL);
  515. if (IS_ERR(mnt))
  516. return PTR_ERR(mnt);
  517. spin_lock(&pin_fs_lock);
  518. if (!*mount)
  519. *mount = mnt;
  520. }
  521. mntget(*mount);
  522. ++*count;
  523. spin_unlock(&pin_fs_lock);
  524. mntput(mnt);
  525. return 0;
  526. }
  527. EXPORT_SYMBOL(simple_pin_fs);
  528. void simple_release_fs(struct vfsmount **mount, int *count)
  529. {
  530. struct vfsmount *mnt;
  531. spin_lock(&pin_fs_lock);
  532. mnt = *mount;
  533. if (!--*count)
  534. *mount = NULL;
  535. spin_unlock(&pin_fs_lock);
  536. mntput(mnt);
  537. }
  538. EXPORT_SYMBOL(simple_release_fs);
  539. /**
  540. * simple_read_from_buffer - copy data from the buffer to user space
  541. * @to: the user space buffer to read to
  542. * @count: the maximum number of bytes to read
  543. * @ppos: the current position in the buffer
  544. * @from: the buffer to read from
  545. * @available: the size of the buffer
  546. *
  547. * The simple_read_from_buffer() function reads up to @count bytes from the
  548. * buffer @from at offset @ppos into the user space address starting at @to.
  549. *
  550. * On success, the number of bytes read is returned and the offset @ppos is
  551. * advanced by this number, or negative value is returned on error.
  552. **/
  553. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  554. const void *from, size_t available)
  555. {
  556. loff_t pos = *ppos;
  557. size_t ret;
  558. if (pos < 0)
  559. return -EINVAL;
  560. if (pos >= available || !count)
  561. return 0;
  562. if (count > available - pos)
  563. count = available - pos;
  564. ret = copy_to_user(to, from + pos, count);
  565. if (ret == count)
  566. return -EFAULT;
  567. count -= ret;
  568. *ppos = pos + count;
  569. return count;
  570. }
  571. EXPORT_SYMBOL(simple_read_from_buffer);
  572. /**
  573. * simple_write_to_buffer - copy data from user space to the buffer
  574. * @to: the buffer to write to
  575. * @available: the size of the buffer
  576. * @ppos: the current position in the buffer
  577. * @from: the user space buffer to read from
  578. * @count: the maximum number of bytes to read
  579. *
  580. * The simple_write_to_buffer() function reads up to @count bytes from the user
  581. * space address starting at @from into the buffer @to at offset @ppos.
  582. *
  583. * On success, the number of bytes written is returned and the offset @ppos is
  584. * advanced by this number, or negative value is returned on error.
  585. **/
  586. ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  587. const void __user *from, size_t count)
  588. {
  589. loff_t pos = *ppos;
  590. size_t res;
  591. if (pos < 0)
  592. return -EINVAL;
  593. if (pos >= available || !count)
  594. return 0;
  595. if (count > available - pos)
  596. count = available - pos;
  597. res = copy_from_user(to + pos, from, count);
  598. if (res == count)
  599. return -EFAULT;
  600. count -= res;
  601. *ppos = pos + count;
  602. return count;
  603. }
  604. EXPORT_SYMBOL(simple_write_to_buffer);
  605. /**
  606. * memory_read_from_buffer - copy data from the buffer
  607. * @to: the kernel space buffer to read to
  608. * @count: the maximum number of bytes to read
  609. * @ppos: the current position in the buffer
  610. * @from: the buffer to read from
  611. * @available: the size of the buffer
  612. *
  613. * The memory_read_from_buffer() function reads up to @count bytes from the
  614. * buffer @from at offset @ppos into the kernel space address starting at @to.
  615. *
  616. * On success, the number of bytes read is returned and the offset @ppos is
  617. * advanced by this number, or negative value is returned on error.
  618. **/
  619. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  620. const void *from, size_t available)
  621. {
  622. loff_t pos = *ppos;
  623. if (pos < 0)
  624. return -EINVAL;
  625. if (pos >= available)
  626. return 0;
  627. if (count > available - pos)
  628. count = available - pos;
  629. memcpy(to, from + pos, count);
  630. *ppos = pos + count;
  631. return count;
  632. }
  633. EXPORT_SYMBOL(memory_read_from_buffer);
  634. /*
  635. * Transaction based IO.
  636. * The file expects a single write which triggers the transaction, and then
  637. * possibly a read which collects the result - which is stored in a
  638. * file-local buffer.
  639. */
  640. void simple_transaction_set(struct file *file, size_t n)
  641. {
  642. struct simple_transaction_argresp *ar = file->private_data;
  643. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  644. /*
  645. * The barrier ensures that ar->size will really remain zero until
  646. * ar->data is ready for reading.
  647. */
  648. smp_mb();
  649. ar->size = n;
  650. }
  651. EXPORT_SYMBOL(simple_transaction_set);
  652. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  653. {
  654. struct simple_transaction_argresp *ar;
  655. static DEFINE_SPINLOCK(simple_transaction_lock);
  656. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  657. return ERR_PTR(-EFBIG);
  658. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  659. if (!ar)
  660. return ERR_PTR(-ENOMEM);
  661. spin_lock(&simple_transaction_lock);
  662. /* only one write allowed per open */
  663. if (file->private_data) {
  664. spin_unlock(&simple_transaction_lock);
  665. free_page((unsigned long)ar);
  666. return ERR_PTR(-EBUSY);
  667. }
  668. file->private_data = ar;
  669. spin_unlock(&simple_transaction_lock);
  670. if (copy_from_user(ar->data, buf, size))
  671. return ERR_PTR(-EFAULT);
  672. return ar->data;
  673. }
  674. EXPORT_SYMBOL(simple_transaction_get);
  675. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  676. {
  677. struct simple_transaction_argresp *ar = file->private_data;
  678. if (!ar)
  679. return 0;
  680. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  681. }
  682. EXPORT_SYMBOL(simple_transaction_read);
  683. int simple_transaction_release(struct inode *inode, struct file *file)
  684. {
  685. free_page((unsigned long)file->private_data);
  686. return 0;
  687. }
  688. EXPORT_SYMBOL(simple_transaction_release);
  689. /* Simple attribute files */
  690. struct simple_attr {
  691. int (*get)(void *, u64 *);
  692. int (*set)(void *, u64);
  693. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  694. char set_buf[24];
  695. void *data;
  696. const char *fmt; /* format for read operation */
  697. struct mutex mutex; /* protects access to these buffers */
  698. };
  699. /* simple_attr_open is called by an actual attribute open file operation
  700. * to set the attribute specific access operations. */
  701. int simple_attr_open(struct inode *inode, struct file *file,
  702. int (*get)(void *, u64 *), int (*set)(void *, u64),
  703. const char *fmt)
  704. {
  705. struct simple_attr *attr;
  706. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  707. if (!attr)
  708. return -ENOMEM;
  709. attr->get = get;
  710. attr->set = set;
  711. attr->data = inode->i_private;
  712. attr->fmt = fmt;
  713. mutex_init(&attr->mutex);
  714. file->private_data = attr;
  715. return nonseekable_open(inode, file);
  716. }
  717. EXPORT_SYMBOL_GPL(simple_attr_open);
  718. int simple_attr_release(struct inode *inode, struct file *file)
  719. {
  720. kfree(file->private_data);
  721. return 0;
  722. }
  723. EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
  724. /* read from the buffer that is filled with the get function */
  725. ssize_t simple_attr_read(struct file *file, char __user *buf,
  726. size_t len, loff_t *ppos)
  727. {
  728. struct simple_attr *attr;
  729. size_t size;
  730. ssize_t ret;
  731. attr = file->private_data;
  732. if (!attr->get)
  733. return -EACCES;
  734. ret = mutex_lock_interruptible(&attr->mutex);
  735. if (ret)
  736. return ret;
  737. if (*ppos) { /* continued read */
  738. size = strlen(attr->get_buf);
  739. } else { /* first read */
  740. u64 val;
  741. ret = attr->get(attr->data, &val);
  742. if (ret)
  743. goto out;
  744. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  745. attr->fmt, (unsigned long long)val);
  746. }
  747. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  748. out:
  749. mutex_unlock(&attr->mutex);
  750. return ret;
  751. }
  752. EXPORT_SYMBOL_GPL(simple_attr_read);
  753. /* interpret the buffer as a number to call the set function with */
  754. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  755. size_t len, loff_t *ppos)
  756. {
  757. struct simple_attr *attr;
  758. u64 val;
  759. size_t size;
  760. ssize_t ret;
  761. attr = file->private_data;
  762. if (!attr->set)
  763. return -EACCES;
  764. ret = mutex_lock_interruptible(&attr->mutex);
  765. if (ret)
  766. return ret;
  767. ret = -EFAULT;
  768. size = min(sizeof(attr->set_buf) - 1, len);
  769. if (copy_from_user(attr->set_buf, buf, size))
  770. goto out;
  771. attr->set_buf[size] = '\0';
  772. val = simple_strtoll(attr->set_buf, NULL, 0);
  773. ret = attr->set(attr->data, val);
  774. if (ret == 0)
  775. ret = len; /* on success, claim we got the whole input */
  776. out:
  777. mutex_unlock(&attr->mutex);
  778. return ret;
  779. }
  780. EXPORT_SYMBOL_GPL(simple_attr_write);
  781. /**
  782. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  783. * @sb: filesystem to do the file handle conversion on
  784. * @fid: file handle to convert
  785. * @fh_len: length of the file handle in bytes
  786. * @fh_type: type of file handle
  787. * @get_inode: filesystem callback to retrieve inode
  788. *
  789. * This function decodes @fid as long as it has one of the well-known
  790. * Linux filehandle types and calls @get_inode on it to retrieve the
  791. * inode for the object specified in the file handle.
  792. */
  793. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  794. int fh_len, int fh_type, struct inode *(*get_inode)
  795. (struct super_block *sb, u64 ino, u32 gen))
  796. {
  797. struct inode *inode = NULL;
  798. if (fh_len < 2)
  799. return NULL;
  800. switch (fh_type) {
  801. case FILEID_INO32_GEN:
  802. case FILEID_INO32_GEN_PARENT:
  803. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  804. break;
  805. }
  806. return d_obtain_alias(inode);
  807. }
  808. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  809. /**
  810. * generic_fh_to_parent - generic helper for the fh_to_parent export operation
  811. * @sb: filesystem to do the file handle conversion on
  812. * @fid: file handle to convert
  813. * @fh_len: length of the file handle in bytes
  814. * @fh_type: type of file handle
  815. * @get_inode: filesystem callback to retrieve inode
  816. *
  817. * This function decodes @fid as long as it has one of the well-known
  818. * Linux filehandle types and calls @get_inode on it to retrieve the
  819. * inode for the _parent_ object specified in the file handle if it
  820. * is specified in the file handle, or NULL otherwise.
  821. */
  822. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  823. int fh_len, int fh_type, struct inode *(*get_inode)
  824. (struct super_block *sb, u64 ino, u32 gen))
  825. {
  826. struct inode *inode = NULL;
  827. if (fh_len <= 2)
  828. return NULL;
  829. switch (fh_type) {
  830. case FILEID_INO32_GEN_PARENT:
  831. inode = get_inode(sb, fid->i32.parent_ino,
  832. (fh_len > 3 ? fid->i32.parent_gen : 0));
  833. break;
  834. }
  835. return d_obtain_alias(inode);
  836. }
  837. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  838. /**
  839. * __generic_file_fsync - generic fsync implementation for simple filesystems
  840. *
  841. * @file: file to synchronize
  842. * @start: start offset in bytes
  843. * @end: end offset in bytes (inclusive)
  844. * @datasync: only synchronize essential metadata if true
  845. *
  846. * This is a generic implementation of the fsync method for simple
  847. * filesystems which track all non-inode metadata in the buffers list
  848. * hanging off the address_space structure.
  849. */
  850. int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
  851. int datasync)
  852. {
  853. struct inode *inode = file->f_mapping->host;
  854. int err;
  855. int ret;
  856. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  857. if (err)
  858. return err;
  859. inode_lock(inode);
  860. ret = sync_mapping_buffers(inode->i_mapping);
  861. if (!(inode->i_state & I_DIRTY_ALL))
  862. goto out;
  863. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  864. goto out;
  865. err = sync_inode_metadata(inode, 1);
  866. if (ret == 0)
  867. ret = err;
  868. out:
  869. inode_unlock(inode);
  870. return ret;
  871. }
  872. EXPORT_SYMBOL(__generic_file_fsync);
  873. /**
  874. * generic_file_fsync - generic fsync implementation for simple filesystems
  875. * with flush
  876. * @file: file to synchronize
  877. * @start: start offset in bytes
  878. * @end: end offset in bytes (inclusive)
  879. * @datasync: only synchronize essential metadata if true
  880. *
  881. */
  882. int generic_file_fsync(struct file *file, loff_t start, loff_t end,
  883. int datasync)
  884. {
  885. struct inode *inode = file->f_mapping->host;
  886. int err;
  887. err = __generic_file_fsync(file, start, end, datasync);
  888. if (err)
  889. return err;
  890. return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  891. }
  892. EXPORT_SYMBOL(generic_file_fsync);
  893. /**
  894. * generic_check_addressable - Check addressability of file system
  895. * @blocksize_bits: log of file system block size
  896. * @num_blocks: number of blocks in file system
  897. *
  898. * Determine whether a file system with @num_blocks blocks (and a
  899. * block size of 2**@blocksize_bits) is addressable by the sector_t
  900. * and page cache of the system. Return 0 if so and -EFBIG otherwise.
  901. */
  902. int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
  903. {
  904. u64 last_fs_block = num_blocks - 1;
  905. u64 last_fs_page =
  906. last_fs_block >> (PAGE_SHIFT - blocksize_bits);
  907. if (unlikely(num_blocks == 0))
  908. return 0;
  909. if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
  910. return -EINVAL;
  911. if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
  912. (last_fs_page > (pgoff_t)(~0ULL))) {
  913. return -EFBIG;
  914. }
  915. return 0;
  916. }
  917. EXPORT_SYMBOL(generic_check_addressable);
  918. /*
  919. * No-op implementation of ->fsync for in-memory filesystems.
  920. */
  921. int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  922. {
  923. return 0;
  924. }
  925. EXPORT_SYMBOL(noop_fsync);
  926. /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
  927. void kfree_link(void *p)
  928. {
  929. kfree(p);
  930. }
  931. EXPORT_SYMBOL(kfree_link);
  932. /*
  933. * nop .set_page_dirty method so that people can use .page_mkwrite on
  934. * anon inodes.
  935. */
  936. static int anon_set_page_dirty(struct page *page)
  937. {
  938. return 0;
  939. };
  940. /*
  941. * A single inode exists for all anon_inode files. Contrary to pipes,
  942. * anon_inode inodes have no associated per-instance data, so we need
  943. * only allocate one of them.
  944. */
  945. struct inode *alloc_anon_inode(struct super_block *s)
  946. {
  947. static const struct address_space_operations anon_aops = {
  948. .set_page_dirty = anon_set_page_dirty,
  949. };
  950. struct inode *inode = new_inode_pseudo(s);
  951. if (!inode)
  952. return ERR_PTR(-ENOMEM);
  953. inode->i_ino = get_next_ino();
  954. inode->i_mapping->a_ops = &anon_aops;
  955. /*
  956. * Mark the inode dirty from the very beginning,
  957. * that way it will never be moved to the dirty
  958. * list because mark_inode_dirty() will think
  959. * that it already _is_ on the dirty list.
  960. */
  961. inode->i_state = I_DIRTY;
  962. inode->i_mode = S_IRUSR | S_IWUSR;
  963. inode->i_uid = current_fsuid();
  964. inode->i_gid = current_fsgid();
  965. inode->i_flags |= S_PRIVATE;
  966. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  967. return inode;
  968. }
  969. EXPORT_SYMBOL(alloc_anon_inode);
  970. /**
  971. * simple_nosetlease - generic helper for prohibiting leases
  972. * @filp: file pointer
  973. * @arg: type of lease to obtain
  974. * @flp: new lease supplied for insertion
  975. * @priv: private data for lm_setup operation
  976. *
  977. * Generic helper for filesystems that do not wish to allow leases to be set.
  978. * All arguments are ignored and it just returns -EINVAL.
  979. */
  980. int
  981. simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
  982. void **priv)
  983. {
  984. return -EINVAL;
  985. }
  986. EXPORT_SYMBOL(simple_nosetlease);
  987. const char *simple_get_link(struct dentry *dentry, struct inode *inode,
  988. struct delayed_call *done)
  989. {
  990. return inode->i_link;
  991. }
  992. EXPORT_SYMBOL(simple_get_link);
  993. const struct inode_operations simple_symlink_inode_operations = {
  994. .get_link = simple_get_link,
  995. };
  996. EXPORT_SYMBOL(simple_symlink_inode_operations);
  997. /*
  998. * Operations for a permanently empty directory.
  999. */
  1000. static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1001. {
  1002. return ERR_PTR(-ENOENT);
  1003. }
  1004. static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1005. struct kstat *stat)
  1006. {
  1007. struct inode *inode = d_inode(dentry);
  1008. generic_fillattr(inode, stat);
  1009. return 0;
  1010. }
  1011. static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
  1012. {
  1013. return -EPERM;
  1014. }
  1015. static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
  1016. {
  1017. return -EOPNOTSUPP;
  1018. }
  1019. static const struct inode_operations empty_dir_inode_operations = {
  1020. .lookup = empty_dir_lookup,
  1021. .permission = generic_permission,
  1022. .setattr = empty_dir_setattr,
  1023. .getattr = empty_dir_getattr,
  1024. .listxattr = empty_dir_listxattr,
  1025. };
  1026. static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
  1027. {
  1028. /* An empty directory has two entries . and .. at offsets 0 and 1 */
  1029. return generic_file_llseek_size(file, offset, whence, 2, 2);
  1030. }
  1031. static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
  1032. {
  1033. dir_emit_dots(file, ctx);
  1034. return 0;
  1035. }
  1036. static const struct file_operations empty_dir_operations = {
  1037. .llseek = empty_dir_llseek,
  1038. .read = generic_read_dir,
  1039. .iterate_shared = empty_dir_readdir,
  1040. .fsync = noop_fsync,
  1041. };
  1042. void make_empty_dir_inode(struct inode *inode)
  1043. {
  1044. set_nlink(inode, 2);
  1045. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  1046. inode->i_uid = GLOBAL_ROOT_UID;
  1047. inode->i_gid = GLOBAL_ROOT_GID;
  1048. inode->i_rdev = 0;
  1049. inode->i_size = 0;
  1050. inode->i_blkbits = PAGE_SHIFT;
  1051. inode->i_blocks = 0;
  1052. inode->i_op = &empty_dir_inode_operations;
  1053. inode->i_opflags &= ~IOP_XATTR;
  1054. inode->i_fop = &empty_dir_operations;
  1055. }
  1056. bool is_empty_dir_inode(struct inode *inode)
  1057. {
  1058. return (inode->i_fop == &empty_dir_operations) &&
  1059. (inode->i_op == &empty_dir_inode_operations);
  1060. }