libfs.c 30 KB

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