libfs.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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. unsigned long long 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. ret = kstrtoull(attr->set_buf, 0, &val);
  776. if (ret)
  777. goto out;
  778. ret = attr->set(attr->data, val);
  779. if (ret == 0)
  780. ret = len; /* on success, claim we got the whole input */
  781. out:
  782. mutex_unlock(&attr->mutex);
  783. return ret;
  784. }
  785. EXPORT_SYMBOL_GPL(simple_attr_write);
  786. /**
  787. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  788. * @sb: filesystem to do the file handle conversion on
  789. * @fid: file handle to convert
  790. * @fh_len: length of the file handle in bytes
  791. * @fh_type: type of file handle
  792. * @get_inode: filesystem callback to retrieve inode
  793. *
  794. * This function decodes @fid as long as it has one of the well-known
  795. * Linux filehandle types and calls @get_inode on it to retrieve the
  796. * inode for the object specified in the file handle.
  797. */
  798. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  799. int fh_len, int fh_type, struct inode *(*get_inode)
  800. (struct super_block *sb, u64 ino, u32 gen))
  801. {
  802. struct inode *inode = NULL;
  803. if (fh_len < 2)
  804. return NULL;
  805. switch (fh_type) {
  806. case FILEID_INO32_GEN:
  807. case FILEID_INO32_GEN_PARENT:
  808. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  809. break;
  810. }
  811. return d_obtain_alias(inode);
  812. }
  813. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  814. /**
  815. * generic_fh_to_parent - generic helper for the fh_to_parent export operation
  816. * @sb: filesystem to do the file handle conversion on
  817. * @fid: file handle to convert
  818. * @fh_len: length of the file handle in bytes
  819. * @fh_type: type of file handle
  820. * @get_inode: filesystem callback to retrieve inode
  821. *
  822. * This function decodes @fid as long as it has one of the well-known
  823. * Linux filehandle types and calls @get_inode on it to retrieve the
  824. * inode for the _parent_ object specified in the file handle if it
  825. * is specified in the file handle, or NULL otherwise.
  826. */
  827. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  828. int fh_len, int fh_type, struct inode *(*get_inode)
  829. (struct super_block *sb, u64 ino, u32 gen))
  830. {
  831. struct inode *inode = NULL;
  832. if (fh_len <= 2)
  833. return NULL;
  834. switch (fh_type) {
  835. case FILEID_INO32_GEN_PARENT:
  836. inode = get_inode(sb, fid->i32.parent_ino,
  837. (fh_len > 3 ? fid->i32.parent_gen : 0));
  838. break;
  839. }
  840. return d_obtain_alias(inode);
  841. }
  842. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  843. /**
  844. * __generic_file_fsync - generic fsync implementation for simple filesystems
  845. *
  846. * @file: file to synchronize
  847. * @start: start offset in bytes
  848. * @end: end offset in bytes (inclusive)
  849. * @datasync: only synchronize essential metadata if true
  850. *
  851. * This is a generic implementation of the fsync method for simple
  852. * filesystems which track all non-inode metadata in the buffers list
  853. * hanging off the address_space structure.
  854. */
  855. int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
  856. int datasync)
  857. {
  858. struct inode *inode = file->f_mapping->host;
  859. int err;
  860. int ret;
  861. err = file_write_and_wait_range(file, start, end);
  862. if (err)
  863. return err;
  864. inode_lock(inode);
  865. ret = sync_mapping_buffers(inode->i_mapping);
  866. if (!(inode->i_state & I_DIRTY_ALL))
  867. goto out;
  868. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  869. goto out;
  870. err = sync_inode_metadata(inode, 1);
  871. if (ret == 0)
  872. ret = err;
  873. out:
  874. inode_unlock(inode);
  875. /* check and advance again to catch errors after syncing out buffers */
  876. err = file_check_and_advance_wb_err(file);
  877. if (ret == 0)
  878. ret = err;
  879. return ret;
  880. }
  881. EXPORT_SYMBOL(__generic_file_fsync);
  882. /**
  883. * generic_file_fsync - generic fsync implementation for simple filesystems
  884. * with flush
  885. * @file: file to synchronize
  886. * @start: start offset in bytes
  887. * @end: end offset in bytes (inclusive)
  888. * @datasync: only synchronize essential metadata if true
  889. *
  890. */
  891. int generic_file_fsync(struct file *file, loff_t start, loff_t end,
  892. int datasync)
  893. {
  894. struct inode *inode = file->f_mapping->host;
  895. int err;
  896. err = __generic_file_fsync(file, start, end, datasync);
  897. if (err)
  898. return err;
  899. return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  900. }
  901. EXPORT_SYMBOL(generic_file_fsync);
  902. /**
  903. * generic_check_addressable - Check addressability of file system
  904. * @blocksize_bits: log of file system block size
  905. * @num_blocks: number of blocks in file system
  906. *
  907. * Determine whether a file system with @num_blocks blocks (and a
  908. * block size of 2**@blocksize_bits) is addressable by the sector_t
  909. * and page cache of the system. Return 0 if so and -EFBIG otherwise.
  910. */
  911. int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
  912. {
  913. u64 last_fs_block = num_blocks - 1;
  914. u64 last_fs_page =
  915. last_fs_block >> (PAGE_SHIFT - blocksize_bits);
  916. if (unlikely(num_blocks == 0))
  917. return 0;
  918. if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
  919. return -EINVAL;
  920. if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
  921. (last_fs_page > (pgoff_t)(~0ULL))) {
  922. return -EFBIG;
  923. }
  924. return 0;
  925. }
  926. EXPORT_SYMBOL(generic_check_addressable);
  927. /*
  928. * No-op implementation of ->fsync for in-memory filesystems.
  929. */
  930. int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  931. {
  932. return 0;
  933. }
  934. EXPORT_SYMBOL(noop_fsync);
  935. int noop_set_page_dirty(struct page *page)
  936. {
  937. /*
  938. * Unlike __set_page_dirty_no_writeback that handles dirty page
  939. * tracking in the page object, dax does all dirty tracking in
  940. * the inode address_space in response to mkwrite faults. In the
  941. * dax case we only need to worry about potentially dirty CPU
  942. * caches, not dirty page cache pages to write back.
  943. *
  944. * This callback is defined to prevent fallback to
  945. * __set_page_dirty_buffers() in set_page_dirty().
  946. */
  947. return 0;
  948. }
  949. EXPORT_SYMBOL_GPL(noop_set_page_dirty);
  950. void noop_invalidatepage(struct page *page, unsigned int offset,
  951. unsigned int length)
  952. {
  953. /*
  954. * There is no page cache to invalidate in the dax case, however
  955. * we need this callback defined to prevent falling back to
  956. * block_invalidatepage() in do_invalidatepage().
  957. */
  958. }
  959. EXPORT_SYMBOL_GPL(noop_invalidatepage);
  960. ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  961. {
  962. /*
  963. * iomap based filesystems support direct I/O without need for
  964. * this callback. However, it still needs to be set in
  965. * inode->a_ops so that open/fcntl know that direct I/O is
  966. * generally supported.
  967. */
  968. return -EINVAL;
  969. }
  970. EXPORT_SYMBOL_GPL(noop_direct_IO);
  971. /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
  972. void kfree_link(void *p)
  973. {
  974. kfree(p);
  975. }
  976. EXPORT_SYMBOL(kfree_link);
  977. /*
  978. * nop .set_page_dirty method so that people can use .page_mkwrite on
  979. * anon inodes.
  980. */
  981. static int anon_set_page_dirty(struct page *page)
  982. {
  983. return 0;
  984. };
  985. /*
  986. * A single inode exists for all anon_inode files. Contrary to pipes,
  987. * anon_inode inodes have no associated per-instance data, so we need
  988. * only allocate one of them.
  989. */
  990. struct inode *alloc_anon_inode(struct super_block *s)
  991. {
  992. static const struct address_space_operations anon_aops = {
  993. .set_page_dirty = anon_set_page_dirty,
  994. };
  995. struct inode *inode = new_inode_pseudo(s);
  996. if (!inode)
  997. return ERR_PTR(-ENOMEM);
  998. inode->i_ino = get_next_ino();
  999. inode->i_mapping->a_ops = &anon_aops;
  1000. /*
  1001. * Mark the inode dirty from the very beginning,
  1002. * that way it will never be moved to the dirty
  1003. * list because mark_inode_dirty() will think
  1004. * that it already _is_ on the dirty list.
  1005. */
  1006. inode->i_state = I_DIRTY;
  1007. inode->i_mode = S_IRUSR | S_IWUSR;
  1008. inode->i_uid = current_fsuid();
  1009. inode->i_gid = current_fsgid();
  1010. inode->i_flags |= S_PRIVATE;
  1011. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  1012. return inode;
  1013. }
  1014. EXPORT_SYMBOL(alloc_anon_inode);
  1015. /**
  1016. * simple_nosetlease - generic helper for prohibiting leases
  1017. * @filp: file pointer
  1018. * @arg: type of lease to obtain
  1019. * @flp: new lease supplied for insertion
  1020. * @priv: private data for lm_setup operation
  1021. *
  1022. * Generic helper for filesystems that do not wish to allow leases to be set.
  1023. * All arguments are ignored and it just returns -EINVAL.
  1024. */
  1025. int
  1026. simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
  1027. void **priv)
  1028. {
  1029. return -EINVAL;
  1030. }
  1031. EXPORT_SYMBOL(simple_nosetlease);
  1032. const char *simple_get_link(struct dentry *dentry, struct inode *inode,
  1033. struct delayed_call *done)
  1034. {
  1035. return inode->i_link;
  1036. }
  1037. EXPORT_SYMBOL(simple_get_link);
  1038. const struct inode_operations simple_symlink_inode_operations = {
  1039. .get_link = simple_get_link,
  1040. };
  1041. EXPORT_SYMBOL(simple_symlink_inode_operations);
  1042. /*
  1043. * Operations for a permanently empty directory.
  1044. */
  1045. static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1046. {
  1047. return ERR_PTR(-ENOENT);
  1048. }
  1049. static int empty_dir_getattr(const struct path *path, struct kstat *stat,
  1050. u32 request_mask, unsigned int query_flags)
  1051. {
  1052. struct inode *inode = d_inode(path->dentry);
  1053. generic_fillattr(inode, stat);
  1054. return 0;
  1055. }
  1056. static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
  1057. {
  1058. return -EPERM;
  1059. }
  1060. static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
  1061. {
  1062. return -EOPNOTSUPP;
  1063. }
  1064. static const struct inode_operations empty_dir_inode_operations = {
  1065. .lookup = empty_dir_lookup,
  1066. .permission = generic_permission,
  1067. .setattr = empty_dir_setattr,
  1068. .getattr = empty_dir_getattr,
  1069. .listxattr = empty_dir_listxattr,
  1070. };
  1071. static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
  1072. {
  1073. /* An empty directory has two entries . and .. at offsets 0 and 1 */
  1074. return generic_file_llseek_size(file, offset, whence, 2, 2);
  1075. }
  1076. static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
  1077. {
  1078. dir_emit_dots(file, ctx);
  1079. return 0;
  1080. }
  1081. static const struct file_operations empty_dir_operations = {
  1082. .llseek = empty_dir_llseek,
  1083. .read = generic_read_dir,
  1084. .iterate_shared = empty_dir_readdir,
  1085. .fsync = noop_fsync,
  1086. };
  1087. void make_empty_dir_inode(struct inode *inode)
  1088. {
  1089. set_nlink(inode, 2);
  1090. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  1091. inode->i_uid = GLOBAL_ROOT_UID;
  1092. inode->i_gid = GLOBAL_ROOT_GID;
  1093. inode->i_rdev = 0;
  1094. inode->i_size = 0;
  1095. inode->i_blkbits = PAGE_SHIFT;
  1096. inode->i_blocks = 0;
  1097. inode->i_op = &empty_dir_inode_operations;
  1098. inode->i_opflags &= ~IOP_XATTR;
  1099. inode->i_fop = &empty_dir_operations;
  1100. }
  1101. bool is_empty_dir_inode(struct inode *inode)
  1102. {
  1103. return (inode->i_fop == &empty_dir_operations) &&
  1104. (inode->i_op == &empty_dir_inode_operations);
  1105. }