libfs.c 25 KB

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