libfs.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * fs/libfs.c
  3. * Library for filesystems writers.
  4. */
  5. #include <linux/export.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/slab.h>
  8. #include <linux/mount.h>
  9. #include <linux/vfs.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/mutex.h>
  12. #include <linux/exportfs.h>
  13. #include <linux/writeback.h>
  14. #include <linux/buffer_head.h> /* sync_mapping_buffers */
  15. #include <asm/uaccess.h>
  16. #include "internal.h"
  17. static inline int simple_positive(struct dentry *dentry)
  18. {
  19. return dentry->d_inode && !d_unhashed(dentry);
  20. }
  21. int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
  22. struct kstat *stat)
  23. {
  24. struct inode *inode = dentry->d_inode;
  25. generic_fillattr(inode, stat);
  26. stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
  27. return 0;
  28. }
  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_CACHE_SIZE;
  33. buf->f_namelen = NAME_MAX;
  34. return 0;
  35. }
  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. static int simple_delete_dentry(const struct dentry *dentry)
  41. {
  42. return 1;
  43. }
  44. /*
  45. * Lookup the data. This is trivial - if the dentry didn't already
  46. * exist, we know it is negative. Set d_op to delete negative dentries.
  47. */
  48. struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  49. {
  50. static const struct dentry_operations simple_dentry_operations = {
  51. .d_delete = simple_delete_dentry,
  52. };
  53. if (dentry->d_name.len > NAME_MAX)
  54. return ERR_PTR(-ENAMETOOLONG);
  55. d_set_d_op(dentry, &simple_dentry_operations);
  56. d_add(dentry, NULL);
  57. return NULL;
  58. }
  59. int dcache_dir_open(struct inode *inode, struct file *file)
  60. {
  61. static struct qstr cursor_name = {.len = 1, .name = "."};
  62. file->private_data = d_alloc(file->f_path.dentry, &cursor_name);
  63. return file->private_data ? 0 : -ENOMEM;
  64. }
  65. int dcache_dir_close(struct inode *inode, struct file *file)
  66. {
  67. dput(file->private_data);
  68. return 0;
  69. }
  70. loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
  71. {
  72. struct dentry *dentry = file->f_path.dentry;
  73. mutex_lock(&dentry->d_inode->i_mutex);
  74. switch (origin) {
  75. case 1:
  76. offset += file->f_pos;
  77. case 0:
  78. if (offset >= 0)
  79. break;
  80. default:
  81. mutex_unlock(&dentry->d_inode->i_mutex);
  82. return -EINVAL;
  83. }
  84. if (offset != file->f_pos) {
  85. file->f_pos = offset;
  86. if (file->f_pos >= 2) {
  87. struct list_head *p;
  88. struct dentry *cursor = file->private_data;
  89. loff_t n = file->f_pos - 2;
  90. spin_lock(&dentry->d_lock);
  91. /* d_lock not required for cursor */
  92. list_del(&cursor->d_u.d_child);
  93. p = dentry->d_subdirs.next;
  94. while (n && p != &dentry->d_subdirs) {
  95. struct dentry *next;
  96. next = list_entry(p, struct dentry, d_u.d_child);
  97. spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
  98. if (simple_positive(next))
  99. n--;
  100. spin_unlock(&next->d_lock);
  101. p = p->next;
  102. }
  103. list_add_tail(&cursor->d_u.d_child, p);
  104. spin_unlock(&dentry->d_lock);
  105. }
  106. }
  107. mutex_unlock(&dentry->d_inode->i_mutex);
  108. return offset;
  109. }
  110. /* Relationship between i_mode and the DT_xxx types */
  111. static inline unsigned char dt_type(struct inode *inode)
  112. {
  113. return (inode->i_mode >> 12) & 15;
  114. }
  115. /*
  116. * Directory is locked and all positive dentries in it are safe, since
  117. * for ramfs-type trees they can't go away without unlink() or rmdir(),
  118. * both impossible due to the lock on directory.
  119. */
  120. int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
  121. {
  122. struct dentry *dentry = filp->f_path.dentry;
  123. struct dentry *cursor = filp->private_data;
  124. struct list_head *p, *q = &cursor->d_u.d_child;
  125. ino_t ino;
  126. int i = filp->f_pos;
  127. switch (i) {
  128. case 0:
  129. ino = dentry->d_inode->i_ino;
  130. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  131. break;
  132. filp->f_pos++;
  133. i++;
  134. /* fallthrough */
  135. case 1:
  136. ino = parent_ino(dentry);
  137. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  138. break;
  139. filp->f_pos++;
  140. i++;
  141. /* fallthrough */
  142. default:
  143. spin_lock(&dentry->d_lock);
  144. if (filp->f_pos == 2)
  145. list_move(q, &dentry->d_subdirs);
  146. for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
  147. struct dentry *next;
  148. next = list_entry(p, struct dentry, d_u.d_child);
  149. spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
  150. if (!simple_positive(next)) {
  151. spin_unlock(&next->d_lock);
  152. continue;
  153. }
  154. spin_unlock(&next->d_lock);
  155. spin_unlock(&dentry->d_lock);
  156. if (filldir(dirent, next->d_name.name,
  157. next->d_name.len, filp->f_pos,
  158. next->d_inode->i_ino,
  159. dt_type(next->d_inode)) < 0)
  160. return 0;
  161. spin_lock(&dentry->d_lock);
  162. spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
  163. /* next is still alive */
  164. list_move(q, p);
  165. spin_unlock(&next->d_lock);
  166. p = q;
  167. filp->f_pos++;
  168. }
  169. spin_unlock(&dentry->d_lock);
  170. }
  171. return 0;
  172. }
  173. ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
  174. {
  175. return -EISDIR;
  176. }
  177. const struct file_operations simple_dir_operations = {
  178. .open = dcache_dir_open,
  179. .release = dcache_dir_close,
  180. .llseek = dcache_dir_lseek,
  181. .read = generic_read_dir,
  182. .readdir = dcache_readdir,
  183. .fsync = noop_fsync,
  184. };
  185. const struct inode_operations simple_dir_inode_operations = {
  186. .lookup = simple_lookup,
  187. };
  188. static const struct super_operations simple_super_operations = {
  189. .statfs = simple_statfs,
  190. };
  191. /*
  192. * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
  193. * will never be mountable)
  194. */
  195. struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,
  196. const struct super_operations *ops,
  197. const struct dentry_operations *dops, unsigned long magic)
  198. {
  199. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  200. struct dentry *dentry;
  201. struct inode *root;
  202. struct qstr d_name = {.name = name, .len = strlen(name)};
  203. if (IS_ERR(s))
  204. return ERR_CAST(s);
  205. s->s_flags = MS_NOUSER;
  206. s->s_maxbytes = MAX_LFS_FILESIZE;
  207. s->s_blocksize = PAGE_SIZE;
  208. s->s_blocksize_bits = PAGE_SHIFT;
  209. s->s_magic = magic;
  210. s->s_op = ops ? ops : &simple_super_operations;
  211. s->s_time_gran = 1;
  212. root = new_inode(s);
  213. if (!root)
  214. goto Enomem;
  215. /*
  216. * since this is the first inode, make it number 1. New inodes created
  217. * after this must take care not to collide with it (by passing
  218. * max_reserved of 1 to iunique).
  219. */
  220. root->i_ino = 1;
  221. root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
  222. root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
  223. dentry = __d_alloc(s, &d_name);
  224. if (!dentry) {
  225. iput(root);
  226. goto Enomem;
  227. }
  228. d_instantiate(dentry, root);
  229. s->s_root = dentry;
  230. s->s_d_op = dops;
  231. s->s_flags |= MS_ACTIVE;
  232. return dget(s->s_root);
  233. Enomem:
  234. deactivate_locked_super(s);
  235. return ERR_PTR(-ENOMEM);
  236. }
  237. int simple_open(struct inode *inode, struct file *file)
  238. {
  239. if (inode->i_private)
  240. file->private_data = inode->i_private;
  241. return 0;
  242. }
  243. int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  244. {
  245. struct inode *inode = old_dentry->d_inode;
  246. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  247. inc_nlink(inode);
  248. ihold(inode);
  249. dget(dentry);
  250. d_instantiate(dentry, inode);
  251. return 0;
  252. }
  253. int simple_empty(struct dentry *dentry)
  254. {
  255. struct dentry *child;
  256. int ret = 0;
  257. spin_lock(&dentry->d_lock);
  258. list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child) {
  259. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  260. if (simple_positive(child)) {
  261. spin_unlock(&child->d_lock);
  262. goto out;
  263. }
  264. spin_unlock(&child->d_lock);
  265. }
  266. ret = 1;
  267. out:
  268. spin_unlock(&dentry->d_lock);
  269. return ret;
  270. }
  271. int simple_unlink(struct inode *dir, struct dentry *dentry)
  272. {
  273. struct inode *inode = dentry->d_inode;
  274. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  275. drop_nlink(inode);
  276. dput(dentry);
  277. return 0;
  278. }
  279. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  280. {
  281. if (!simple_empty(dentry))
  282. return -ENOTEMPTY;
  283. drop_nlink(dentry->d_inode);
  284. simple_unlink(dir, dentry);
  285. drop_nlink(dir);
  286. return 0;
  287. }
  288. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  289. struct inode *new_dir, struct dentry *new_dentry)
  290. {
  291. struct inode *inode = old_dentry->d_inode;
  292. int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
  293. if (!simple_empty(new_dentry))
  294. return -ENOTEMPTY;
  295. if (new_dentry->d_inode) {
  296. simple_unlink(new_dir, new_dentry);
  297. if (they_are_dirs) {
  298. drop_nlink(new_dentry->d_inode);
  299. drop_nlink(old_dir);
  300. }
  301. } else if (they_are_dirs) {
  302. drop_nlink(old_dir);
  303. inc_nlink(new_dir);
  304. }
  305. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  306. new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
  307. return 0;
  308. }
  309. /**
  310. * simple_setattr - setattr for simple filesystem
  311. * @dentry: dentry
  312. * @iattr: iattr structure
  313. *
  314. * Returns 0 on success, -error on failure.
  315. *
  316. * simple_setattr is a simple ->setattr implementation without a proper
  317. * implementation of size changes.
  318. *
  319. * It can either be used for in-memory filesystems or special files
  320. * on simple regular filesystems. Anything that needs to change on-disk
  321. * or wire state on size changes needs its own setattr method.
  322. */
  323. int simple_setattr(struct dentry *dentry, struct iattr *iattr)
  324. {
  325. struct inode *inode = dentry->d_inode;
  326. int error;
  327. WARN_ON_ONCE(inode->i_op->truncate);
  328. error = inode_change_ok(inode, iattr);
  329. if (error)
  330. return error;
  331. if (iattr->ia_valid & ATTR_SIZE)
  332. truncate_setsize(inode, iattr->ia_size);
  333. setattr_copy(inode, iattr);
  334. mark_inode_dirty(inode);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(simple_setattr);
  338. int simple_readpage(struct file *file, struct page *page)
  339. {
  340. clear_highpage(page);
  341. flush_dcache_page(page);
  342. SetPageUptodate(page);
  343. unlock_page(page);
  344. return 0;
  345. }
  346. int simple_write_begin(struct file *file, struct address_space *mapping,
  347. loff_t pos, unsigned len, unsigned flags,
  348. struct page **pagep, void **fsdata)
  349. {
  350. struct page *page;
  351. pgoff_t index;
  352. index = pos >> PAGE_CACHE_SHIFT;
  353. page = grab_cache_page_write_begin(mapping, index, flags);
  354. if (!page)
  355. return -ENOMEM;
  356. *pagep = page;
  357. if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
  358. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  359. zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE);
  360. }
  361. return 0;
  362. }
  363. /**
  364. * simple_write_end - .write_end helper for non-block-device FSes
  365. * @available: See .write_end of address_space_operations
  366. * @file: "
  367. * @mapping: "
  368. * @pos: "
  369. * @len: "
  370. * @copied: "
  371. * @page: "
  372. * @fsdata: "
  373. *
  374. * simple_write_end does the minimum needed for updating a page after writing is
  375. * done. It has the same API signature as the .write_end of
  376. * address_space_operations vector. So it can just be set onto .write_end for
  377. * FSes that don't need any other processing. i_mutex is assumed to be held.
  378. * Block based filesystems should use generic_write_end().
  379. * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
  380. * is not called, so a filesystem that actually does store data in .write_inode
  381. * should extend on what's done here with a call to mark_inode_dirty() in the
  382. * case that i_size has changed.
  383. */
  384. int simple_write_end(struct file *file, struct address_space *mapping,
  385. loff_t pos, unsigned len, unsigned copied,
  386. struct page *page, void *fsdata)
  387. {
  388. struct inode *inode = page->mapping->host;
  389. loff_t last_pos = pos + copied;
  390. /* zero the stale part of the page if we did a short copy */
  391. if (copied < len) {
  392. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  393. zero_user(page, from + copied, len - copied);
  394. }
  395. if (!PageUptodate(page))
  396. SetPageUptodate(page);
  397. /*
  398. * No need to use i_size_read() here, the i_size
  399. * cannot change under us because we hold the i_mutex.
  400. */
  401. if (last_pos > inode->i_size)
  402. i_size_write(inode, last_pos);
  403. set_page_dirty(page);
  404. unlock_page(page);
  405. page_cache_release(page);
  406. return copied;
  407. }
  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. dput(root);
  468. return -ENOMEM;
  469. }
  470. static DEFINE_SPINLOCK(pin_fs_lock);
  471. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  472. {
  473. struct vfsmount *mnt = NULL;
  474. spin_lock(&pin_fs_lock);
  475. if (unlikely(!*mount)) {
  476. spin_unlock(&pin_fs_lock);
  477. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL);
  478. if (IS_ERR(mnt))
  479. return PTR_ERR(mnt);
  480. spin_lock(&pin_fs_lock);
  481. if (!*mount)
  482. *mount = mnt;
  483. }
  484. mntget(*mount);
  485. ++*count;
  486. spin_unlock(&pin_fs_lock);
  487. mntput(mnt);
  488. return 0;
  489. }
  490. void simple_release_fs(struct vfsmount **mount, int *count)
  491. {
  492. struct vfsmount *mnt;
  493. spin_lock(&pin_fs_lock);
  494. mnt = *mount;
  495. if (!--*count)
  496. *mount = NULL;
  497. spin_unlock(&pin_fs_lock);
  498. mntput(mnt);
  499. }
  500. /**
  501. * simple_read_from_buffer - copy data from the buffer to user space
  502. * @to: the user space buffer to read to
  503. * @count: the maximum number of bytes to read
  504. * @ppos: the current position in the buffer
  505. * @from: the buffer to read from
  506. * @available: the size of the buffer
  507. *
  508. * The simple_read_from_buffer() function reads up to @count bytes from the
  509. * buffer @from at offset @ppos into the user space address starting at @to.
  510. *
  511. * On success, the number of bytes read is returned and the offset @ppos is
  512. * advanced by this number, or negative value is returned on error.
  513. **/
  514. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  515. const void *from, size_t available)
  516. {
  517. loff_t pos = *ppos;
  518. size_t ret;
  519. if (pos < 0)
  520. return -EINVAL;
  521. if (pos >= available || !count)
  522. return 0;
  523. if (count > available - pos)
  524. count = available - pos;
  525. ret = copy_to_user(to, from + pos, count);
  526. if (ret == count)
  527. return -EFAULT;
  528. count -= ret;
  529. *ppos = pos + count;
  530. return count;
  531. }
  532. /**
  533. * simple_write_to_buffer - copy data from user space to the buffer
  534. * @to: the buffer to write to
  535. * @available: the size of the buffer
  536. * @ppos: the current position in the buffer
  537. * @from: the user space buffer to read from
  538. * @count: the maximum number of bytes to read
  539. *
  540. * The simple_write_to_buffer() function reads up to @count bytes from the user
  541. * space address starting at @from into the buffer @to at offset @ppos.
  542. *
  543. * On success, the number of bytes written is returned and the offset @ppos is
  544. * advanced by this number, or negative value is returned on error.
  545. **/
  546. ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  547. const void __user *from, size_t count)
  548. {
  549. loff_t pos = *ppos;
  550. size_t res;
  551. if (pos < 0)
  552. return -EINVAL;
  553. if (pos >= available || !count)
  554. return 0;
  555. if (count > available - pos)
  556. count = available - pos;
  557. res = copy_from_user(to + pos, from, count);
  558. if (res == count)
  559. return -EFAULT;
  560. count -= res;
  561. *ppos = pos + count;
  562. return count;
  563. }
  564. /**
  565. * memory_read_from_buffer - copy data from the buffer
  566. * @to: the kernel space buffer to read to
  567. * @count: the maximum number of bytes to read
  568. * @ppos: the current position in the buffer
  569. * @from: the buffer to read from
  570. * @available: the size of the buffer
  571. *
  572. * The memory_read_from_buffer() function reads up to @count bytes from the
  573. * buffer @from at offset @ppos into the kernel space address starting at @to.
  574. *
  575. * On success, the number of bytes read is returned and the offset @ppos is
  576. * advanced by this number, or negative value is returned on error.
  577. **/
  578. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  579. const void *from, size_t available)
  580. {
  581. loff_t pos = *ppos;
  582. if (pos < 0)
  583. return -EINVAL;
  584. if (pos >= available)
  585. return 0;
  586. if (count > available - pos)
  587. count = available - pos;
  588. memcpy(to, from + pos, count);
  589. *ppos = pos + count;
  590. return count;
  591. }
  592. /*
  593. * Transaction based IO.
  594. * The file expects a single write which triggers the transaction, and then
  595. * possibly a read which collects the result - which is stored in a
  596. * file-local buffer.
  597. */
  598. void simple_transaction_set(struct file *file, size_t n)
  599. {
  600. struct simple_transaction_argresp *ar = file->private_data;
  601. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  602. /*
  603. * The barrier ensures that ar->size will really remain zero until
  604. * ar->data is ready for reading.
  605. */
  606. smp_mb();
  607. ar->size = n;
  608. }
  609. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  610. {
  611. struct simple_transaction_argresp *ar;
  612. static DEFINE_SPINLOCK(simple_transaction_lock);
  613. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  614. return ERR_PTR(-EFBIG);
  615. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  616. if (!ar)
  617. return ERR_PTR(-ENOMEM);
  618. spin_lock(&simple_transaction_lock);
  619. /* only one write allowed per open */
  620. if (file->private_data) {
  621. spin_unlock(&simple_transaction_lock);
  622. free_page((unsigned long)ar);
  623. return ERR_PTR(-EBUSY);
  624. }
  625. file->private_data = ar;
  626. spin_unlock(&simple_transaction_lock);
  627. if (copy_from_user(ar->data, buf, size))
  628. return ERR_PTR(-EFAULT);
  629. return ar->data;
  630. }
  631. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  632. {
  633. struct simple_transaction_argresp *ar = file->private_data;
  634. if (!ar)
  635. return 0;
  636. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  637. }
  638. int simple_transaction_release(struct inode *inode, struct file *file)
  639. {
  640. free_page((unsigned long)file->private_data);
  641. return 0;
  642. }
  643. /* Simple attribute files */
  644. struct simple_attr {
  645. int (*get)(void *, u64 *);
  646. int (*set)(void *, u64);
  647. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  648. char set_buf[24];
  649. void *data;
  650. const char *fmt; /* format for read operation */
  651. struct mutex mutex; /* protects access to these buffers */
  652. };
  653. /* simple_attr_open is called by an actual attribute open file operation
  654. * to set the attribute specific access operations. */
  655. int simple_attr_open(struct inode *inode, struct file *file,
  656. int (*get)(void *, u64 *), int (*set)(void *, u64),
  657. const char *fmt)
  658. {
  659. struct simple_attr *attr;
  660. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  661. if (!attr)
  662. return -ENOMEM;
  663. attr->get = get;
  664. attr->set = set;
  665. attr->data = inode->i_private;
  666. attr->fmt = fmt;
  667. mutex_init(&attr->mutex);
  668. file->private_data = attr;
  669. return nonseekable_open(inode, file);
  670. }
  671. int simple_attr_release(struct inode *inode, struct file *file)
  672. {
  673. kfree(file->private_data);
  674. return 0;
  675. }
  676. /* read from the buffer that is filled with the get function */
  677. ssize_t simple_attr_read(struct file *file, char __user *buf,
  678. size_t len, loff_t *ppos)
  679. {
  680. struct simple_attr *attr;
  681. size_t size;
  682. ssize_t ret;
  683. attr = file->private_data;
  684. if (!attr->get)
  685. return -EACCES;
  686. ret = mutex_lock_interruptible(&attr->mutex);
  687. if (ret)
  688. return ret;
  689. if (*ppos) { /* continued read */
  690. size = strlen(attr->get_buf);
  691. } else { /* first read */
  692. u64 val;
  693. ret = attr->get(attr->data, &val);
  694. if (ret)
  695. goto out;
  696. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  697. attr->fmt, (unsigned long long)val);
  698. }
  699. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  700. out:
  701. mutex_unlock(&attr->mutex);
  702. return ret;
  703. }
  704. /* interpret the buffer as a number to call the set function with */
  705. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  706. size_t len, loff_t *ppos)
  707. {
  708. struct simple_attr *attr;
  709. u64 val;
  710. size_t size;
  711. ssize_t ret;
  712. attr = file->private_data;
  713. if (!attr->set)
  714. return -EACCES;
  715. ret = mutex_lock_interruptible(&attr->mutex);
  716. if (ret)
  717. return ret;
  718. ret = -EFAULT;
  719. size = min(sizeof(attr->set_buf) - 1, len);
  720. if (copy_from_user(attr->set_buf, buf, size))
  721. goto out;
  722. attr->set_buf[size] = '\0';
  723. val = simple_strtoll(attr->set_buf, NULL, 0);
  724. ret = attr->set(attr->data, val);
  725. if (ret == 0)
  726. ret = len; /* on success, claim we got the whole input */
  727. out:
  728. mutex_unlock(&attr->mutex);
  729. return ret;
  730. }
  731. /**
  732. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  733. * @sb: filesystem to do the file handle conversion on
  734. * @fid: file handle to convert
  735. * @fh_len: length of the file handle in bytes
  736. * @fh_type: type of file handle
  737. * @get_inode: filesystem callback to retrieve inode
  738. *
  739. * This function decodes @fid as long as it has one of the well-known
  740. * Linux filehandle types and calls @get_inode on it to retrieve the
  741. * inode for the object specified in the file handle.
  742. */
  743. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  744. int fh_len, int fh_type, struct inode *(*get_inode)
  745. (struct super_block *sb, u64 ino, u32 gen))
  746. {
  747. struct inode *inode = NULL;
  748. if (fh_len < 2)
  749. return NULL;
  750. switch (fh_type) {
  751. case FILEID_INO32_GEN:
  752. case FILEID_INO32_GEN_PARENT:
  753. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  754. break;
  755. }
  756. return d_obtain_alias(inode);
  757. }
  758. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  759. /**
  760. * generic_fh_to_dentry - generic helper for the fh_to_parent export operation
  761. * @sb: filesystem to do the file handle conversion on
  762. * @fid: file handle to convert
  763. * @fh_len: length of the file handle in bytes
  764. * @fh_type: type of file handle
  765. * @get_inode: filesystem callback to retrieve inode
  766. *
  767. * This function decodes @fid as long as it has one of the well-known
  768. * Linux filehandle types and calls @get_inode on it to retrieve the
  769. * inode for the _parent_ object specified in the file handle if it
  770. * is specified in the file handle, or NULL otherwise.
  771. */
  772. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  773. int fh_len, int fh_type, struct inode *(*get_inode)
  774. (struct super_block *sb, u64 ino, u32 gen))
  775. {
  776. struct inode *inode = NULL;
  777. if (fh_len <= 2)
  778. return NULL;
  779. switch (fh_type) {
  780. case FILEID_INO32_GEN_PARENT:
  781. inode = get_inode(sb, fid->i32.parent_ino,
  782. (fh_len > 3 ? fid->i32.parent_gen : 0));
  783. break;
  784. }
  785. return d_obtain_alias(inode);
  786. }
  787. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  788. /**
  789. * generic_file_fsync - generic fsync implementation for simple filesystems
  790. * @file: file to synchronize
  791. * @datasync: only synchronize essential metadata if true
  792. *
  793. * This is a generic implementation of the fsync method for simple
  794. * filesystems which track all non-inode metadata in the buffers list
  795. * hanging off the address_space structure.
  796. */
  797. int generic_file_fsync(struct file *file, loff_t start, loff_t end,
  798. int datasync)
  799. {
  800. struct inode *inode = file->f_mapping->host;
  801. int err;
  802. int ret;
  803. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  804. if (err)
  805. return err;
  806. mutex_lock(&inode->i_mutex);
  807. ret = sync_mapping_buffers(inode->i_mapping);
  808. if (!(inode->i_state & I_DIRTY))
  809. goto out;
  810. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  811. goto out;
  812. err = sync_inode_metadata(inode, 1);
  813. if (ret == 0)
  814. ret = err;
  815. out:
  816. mutex_unlock(&inode->i_mutex);
  817. return ret;
  818. }
  819. EXPORT_SYMBOL(generic_file_fsync);
  820. /**
  821. * generic_check_addressable - Check addressability of file system
  822. * @blocksize_bits: log of file system block size
  823. * @num_blocks: number of blocks in file system
  824. *
  825. * Determine whether a file system with @num_blocks blocks (and a
  826. * block size of 2**@blocksize_bits) is addressable by the sector_t
  827. * and page cache of the system. Return 0 if so and -EFBIG otherwise.
  828. */
  829. int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
  830. {
  831. u64 last_fs_block = num_blocks - 1;
  832. u64 last_fs_page =
  833. last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
  834. if (unlikely(num_blocks == 0))
  835. return 0;
  836. if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT))
  837. return -EINVAL;
  838. if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
  839. (last_fs_page > (pgoff_t)(~0ULL))) {
  840. return -EFBIG;
  841. }
  842. return 0;
  843. }
  844. EXPORT_SYMBOL(generic_check_addressable);
  845. /*
  846. * No-op implementation of ->fsync for in-memory filesystems.
  847. */
  848. int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  849. {
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(dcache_dir_close);
  853. EXPORT_SYMBOL(dcache_dir_lseek);
  854. EXPORT_SYMBOL(dcache_dir_open);
  855. EXPORT_SYMBOL(dcache_readdir);
  856. EXPORT_SYMBOL(generic_read_dir);
  857. EXPORT_SYMBOL(mount_pseudo);
  858. EXPORT_SYMBOL(simple_write_begin);
  859. EXPORT_SYMBOL(simple_write_end);
  860. EXPORT_SYMBOL(simple_dir_inode_operations);
  861. EXPORT_SYMBOL(simple_dir_operations);
  862. EXPORT_SYMBOL(simple_empty);
  863. EXPORT_SYMBOL(simple_fill_super);
  864. EXPORT_SYMBOL(simple_getattr);
  865. EXPORT_SYMBOL(simple_open);
  866. EXPORT_SYMBOL(simple_link);
  867. EXPORT_SYMBOL(simple_lookup);
  868. EXPORT_SYMBOL(simple_pin_fs);
  869. EXPORT_SYMBOL(simple_readpage);
  870. EXPORT_SYMBOL(simple_release_fs);
  871. EXPORT_SYMBOL(simple_rename);
  872. EXPORT_SYMBOL(simple_rmdir);
  873. EXPORT_SYMBOL(simple_statfs);
  874. EXPORT_SYMBOL(noop_fsync);
  875. EXPORT_SYMBOL(simple_unlink);
  876. EXPORT_SYMBOL(simple_read_from_buffer);
  877. EXPORT_SYMBOL(simple_write_to_buffer);
  878. EXPORT_SYMBOL(memory_read_from_buffer);
  879. EXPORT_SYMBOL(simple_transaction_set);
  880. EXPORT_SYMBOL(simple_transaction_get);
  881. EXPORT_SYMBOL(simple_transaction_read);
  882. EXPORT_SYMBOL(simple_transaction_release);
  883. EXPORT_SYMBOL_GPL(simple_attr_open);
  884. EXPORT_SYMBOL_GPL(simple_attr_release);
  885. EXPORT_SYMBOL_GPL(simple_attr_read);
  886. EXPORT_SYMBOL_GPL(simple_attr_write);