libfs.c 32 KB

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