libfs.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. #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_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  238. {
  239. struct inode *inode = old_dentry->d_inode;
  240. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  241. inc_nlink(inode);
  242. ihold(inode);
  243. dget(dentry);
  244. d_instantiate(dentry, inode);
  245. return 0;
  246. }
  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_u.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. int simple_unlink(struct inode *dir, struct dentry *dentry)
  266. {
  267. struct inode *inode = dentry->d_inode;
  268. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  269. drop_nlink(inode);
  270. dput(dentry);
  271. return 0;
  272. }
  273. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  274. {
  275. if (!simple_empty(dentry))
  276. return -ENOTEMPTY;
  277. drop_nlink(dentry->d_inode);
  278. simple_unlink(dir, dentry);
  279. drop_nlink(dir);
  280. return 0;
  281. }
  282. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  283. struct inode *new_dir, struct dentry *new_dentry)
  284. {
  285. struct inode *inode = old_dentry->d_inode;
  286. int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
  287. if (!simple_empty(new_dentry))
  288. return -ENOTEMPTY;
  289. if (new_dentry->d_inode) {
  290. simple_unlink(new_dir, new_dentry);
  291. if (they_are_dirs) {
  292. drop_nlink(new_dentry->d_inode);
  293. drop_nlink(old_dir);
  294. }
  295. } else if (they_are_dirs) {
  296. drop_nlink(old_dir);
  297. inc_nlink(new_dir);
  298. }
  299. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  300. new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
  301. return 0;
  302. }
  303. /**
  304. * simple_setattr - setattr for simple filesystem
  305. * @dentry: dentry
  306. * @iattr: iattr structure
  307. *
  308. * Returns 0 on success, -error on failure.
  309. *
  310. * simple_setattr is a simple ->setattr implementation without a proper
  311. * implementation of size changes.
  312. *
  313. * It can either be used for in-memory filesystems or special files
  314. * on simple regular filesystems. Anything that needs to change on-disk
  315. * or wire state on size changes needs its own setattr method.
  316. */
  317. int simple_setattr(struct dentry *dentry, struct iattr *iattr)
  318. {
  319. struct inode *inode = dentry->d_inode;
  320. int error;
  321. WARN_ON_ONCE(inode->i_op->truncate);
  322. error = inode_change_ok(inode, iattr);
  323. if (error)
  324. return error;
  325. if (iattr->ia_valid & ATTR_SIZE)
  326. truncate_setsize(inode, iattr->ia_size);
  327. setattr_copy(inode, iattr);
  328. mark_inode_dirty(inode);
  329. return 0;
  330. }
  331. EXPORT_SYMBOL(simple_setattr);
  332. int simple_readpage(struct file *file, struct page *page)
  333. {
  334. clear_highpage(page);
  335. flush_dcache_page(page);
  336. SetPageUptodate(page);
  337. unlock_page(page);
  338. return 0;
  339. }
  340. int simple_write_begin(struct file *file, struct address_space *mapping,
  341. loff_t pos, unsigned len, unsigned flags,
  342. struct page **pagep, void **fsdata)
  343. {
  344. struct page *page;
  345. pgoff_t index;
  346. index = pos >> PAGE_CACHE_SHIFT;
  347. page = grab_cache_page_write_begin(mapping, index, flags);
  348. if (!page)
  349. return -ENOMEM;
  350. *pagep = page;
  351. if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
  352. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  353. zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE);
  354. }
  355. return 0;
  356. }
  357. /**
  358. * simple_write_end - .write_end helper for non-block-device FSes
  359. * @available: See .write_end of address_space_operations
  360. * @file: "
  361. * @mapping: "
  362. * @pos: "
  363. * @len: "
  364. * @copied: "
  365. * @page: "
  366. * @fsdata: "
  367. *
  368. * simple_write_end does the minimum needed for updating a page after writing is
  369. * done. It has the same API signature as the .write_end of
  370. * address_space_operations vector. So it can just be set onto .write_end for
  371. * FSes that don't need any other processing. i_mutex is assumed to be held.
  372. * Block based filesystems should use generic_write_end().
  373. * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
  374. * is not called, so a filesystem that actually does store data in .write_inode
  375. * should extend on what's done here with a call to mark_inode_dirty() in the
  376. * case that i_size has changed.
  377. */
  378. int simple_write_end(struct file *file, struct address_space *mapping,
  379. loff_t pos, unsigned len, unsigned copied,
  380. struct page *page, void *fsdata)
  381. {
  382. struct inode *inode = page->mapping->host;
  383. loff_t last_pos = pos + copied;
  384. /* zero the stale part of the page if we did a short copy */
  385. if (copied < len) {
  386. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  387. zero_user(page, from + copied, len - copied);
  388. }
  389. if (!PageUptodate(page))
  390. SetPageUptodate(page);
  391. /*
  392. * No need to use i_size_read() here, the i_size
  393. * cannot change under us because we hold the i_mutex.
  394. */
  395. if (last_pos > inode->i_size)
  396. i_size_write(inode, last_pos);
  397. set_page_dirty(page);
  398. unlock_page(page);
  399. page_cache_release(page);
  400. return copied;
  401. }
  402. /*
  403. * the inodes created here are not hashed. If you use iunique to generate
  404. * unique inode values later for this filesystem, then you must take care
  405. * to pass it an appropriate max_reserved value to avoid collisions.
  406. */
  407. int simple_fill_super(struct super_block *s, unsigned long magic,
  408. struct tree_descr *files)
  409. {
  410. struct inode *inode;
  411. struct dentry *root;
  412. struct dentry *dentry;
  413. int i;
  414. s->s_blocksize = PAGE_CACHE_SIZE;
  415. s->s_blocksize_bits = PAGE_CACHE_SHIFT;
  416. s->s_magic = magic;
  417. s->s_op = &simple_super_operations;
  418. s->s_time_gran = 1;
  419. inode = new_inode(s);
  420. if (!inode)
  421. return -ENOMEM;
  422. /*
  423. * because the root inode is 1, the files array must not contain an
  424. * entry at index 1
  425. */
  426. inode->i_ino = 1;
  427. inode->i_mode = S_IFDIR | 0755;
  428. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  429. inode->i_op = &simple_dir_inode_operations;
  430. inode->i_fop = &simple_dir_operations;
  431. inode->i_nlink = 2;
  432. root = d_alloc_root(inode);
  433. if (!root) {
  434. iput(inode);
  435. return -ENOMEM;
  436. }
  437. for (i = 0; !files->name || files->name[0]; i++, files++) {
  438. if (!files->name)
  439. continue;
  440. /* warn if it tries to conflict with the root inode */
  441. if (unlikely(i == 1))
  442. printk(KERN_WARNING "%s: %s passed in a files array"
  443. "with an index of 1!\n", __func__,
  444. s->s_type->name);
  445. dentry = d_alloc_name(root, files->name);
  446. if (!dentry)
  447. goto out;
  448. inode = new_inode(s);
  449. if (!inode)
  450. goto out;
  451. inode->i_mode = S_IFREG | files->mode;
  452. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  453. inode->i_fop = files->ops;
  454. inode->i_ino = i;
  455. d_add(dentry, inode);
  456. }
  457. s->s_root = root;
  458. return 0;
  459. out:
  460. d_genocide(root);
  461. dput(root);
  462. return -ENOMEM;
  463. }
  464. static DEFINE_SPINLOCK(pin_fs_lock);
  465. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  466. {
  467. struct vfsmount *mnt = NULL;
  468. spin_lock(&pin_fs_lock);
  469. if (unlikely(!*mount)) {
  470. spin_unlock(&pin_fs_lock);
  471. mnt = vfs_kern_mount(type, 0, type->name, NULL);
  472. if (IS_ERR(mnt))
  473. return PTR_ERR(mnt);
  474. spin_lock(&pin_fs_lock);
  475. if (!*mount)
  476. *mount = mnt;
  477. }
  478. mntget(*mount);
  479. ++*count;
  480. spin_unlock(&pin_fs_lock);
  481. mntput(mnt);
  482. return 0;
  483. }
  484. void simple_release_fs(struct vfsmount **mount, int *count)
  485. {
  486. struct vfsmount *mnt;
  487. spin_lock(&pin_fs_lock);
  488. mnt = *mount;
  489. if (!--*count)
  490. *mount = NULL;
  491. spin_unlock(&pin_fs_lock);
  492. mntput(mnt);
  493. }
  494. /**
  495. * simple_read_from_buffer - copy data from the buffer to user space
  496. * @to: the user space buffer to read to
  497. * @count: the maximum number of bytes to read
  498. * @ppos: the current position in the buffer
  499. * @from: the buffer to read from
  500. * @available: the size of the buffer
  501. *
  502. * The simple_read_from_buffer() function reads up to @count bytes from the
  503. * buffer @from at offset @ppos into the user space address starting at @to.
  504. *
  505. * On success, the number of bytes read is returned and the offset @ppos is
  506. * advanced by this number, or negative value is returned on error.
  507. **/
  508. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  509. const void *from, size_t available)
  510. {
  511. loff_t pos = *ppos;
  512. size_t ret;
  513. if (pos < 0)
  514. return -EINVAL;
  515. if (pos >= available || !count)
  516. return 0;
  517. if (count > available - pos)
  518. count = available - pos;
  519. ret = copy_to_user(to, from + pos, count);
  520. if (ret == count)
  521. return -EFAULT;
  522. count -= ret;
  523. *ppos = pos + count;
  524. return count;
  525. }
  526. /**
  527. * simple_write_to_buffer - copy data from user space to the buffer
  528. * @to: the buffer to write to
  529. * @available: the size of the buffer
  530. * @ppos: the current position in the buffer
  531. * @from: the user space buffer to read from
  532. * @count: the maximum number of bytes to read
  533. *
  534. * The simple_write_to_buffer() function reads up to @count bytes from the user
  535. * space address starting at @from into the buffer @to at offset @ppos.
  536. *
  537. * On success, the number of bytes written is returned and the offset @ppos is
  538. * advanced by this number, or negative value is returned on error.
  539. **/
  540. ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  541. const void __user *from, size_t count)
  542. {
  543. loff_t pos = *ppos;
  544. size_t res;
  545. if (pos < 0)
  546. return -EINVAL;
  547. if (pos >= available || !count)
  548. return 0;
  549. if (count > available - pos)
  550. count = available - pos;
  551. res = copy_from_user(to + pos, from, count);
  552. if (res == count)
  553. return -EFAULT;
  554. count -= res;
  555. *ppos = pos + count;
  556. return count;
  557. }
  558. /**
  559. * memory_read_from_buffer - copy data from the buffer
  560. * @to: the kernel space buffer to read to
  561. * @count: the maximum number of bytes to read
  562. * @ppos: the current position in the buffer
  563. * @from: the buffer to read from
  564. * @available: the size of the buffer
  565. *
  566. * The memory_read_from_buffer() function reads up to @count bytes from the
  567. * buffer @from at offset @ppos into the kernel space address starting at @to.
  568. *
  569. * On success, the number of bytes read is returned and the offset @ppos is
  570. * advanced by this number, or negative value is returned on error.
  571. **/
  572. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  573. const void *from, size_t available)
  574. {
  575. loff_t pos = *ppos;
  576. if (pos < 0)
  577. return -EINVAL;
  578. if (pos >= available)
  579. return 0;
  580. if (count > available - pos)
  581. count = available - pos;
  582. memcpy(to, from + pos, count);
  583. *ppos = pos + count;
  584. return count;
  585. }
  586. /*
  587. * Transaction based IO.
  588. * The file expects a single write which triggers the transaction, and then
  589. * possibly a read which collects the result - which is stored in a
  590. * file-local buffer.
  591. */
  592. void simple_transaction_set(struct file *file, size_t n)
  593. {
  594. struct simple_transaction_argresp *ar = file->private_data;
  595. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  596. /*
  597. * The barrier ensures that ar->size will really remain zero until
  598. * ar->data is ready for reading.
  599. */
  600. smp_mb();
  601. ar->size = n;
  602. }
  603. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  604. {
  605. struct simple_transaction_argresp *ar;
  606. static DEFINE_SPINLOCK(simple_transaction_lock);
  607. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  608. return ERR_PTR(-EFBIG);
  609. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  610. if (!ar)
  611. return ERR_PTR(-ENOMEM);
  612. spin_lock(&simple_transaction_lock);
  613. /* only one write allowed per open */
  614. if (file->private_data) {
  615. spin_unlock(&simple_transaction_lock);
  616. free_page((unsigned long)ar);
  617. return ERR_PTR(-EBUSY);
  618. }
  619. file->private_data = ar;
  620. spin_unlock(&simple_transaction_lock);
  621. if (copy_from_user(ar->data, buf, size))
  622. return ERR_PTR(-EFAULT);
  623. return ar->data;
  624. }
  625. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  626. {
  627. struct simple_transaction_argresp *ar = file->private_data;
  628. if (!ar)
  629. return 0;
  630. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  631. }
  632. int simple_transaction_release(struct inode *inode, struct file *file)
  633. {
  634. free_page((unsigned long)file->private_data);
  635. return 0;
  636. }
  637. /* Simple attribute files */
  638. struct simple_attr {
  639. int (*get)(void *, u64 *);
  640. int (*set)(void *, u64);
  641. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  642. char set_buf[24];
  643. void *data;
  644. const char *fmt; /* format for read operation */
  645. struct mutex mutex; /* protects access to these buffers */
  646. };
  647. /* simple_attr_open is called by an actual attribute open file operation
  648. * to set the attribute specific access operations. */
  649. int simple_attr_open(struct inode *inode, struct file *file,
  650. int (*get)(void *, u64 *), int (*set)(void *, u64),
  651. const char *fmt)
  652. {
  653. struct simple_attr *attr;
  654. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  655. if (!attr)
  656. return -ENOMEM;
  657. attr->get = get;
  658. attr->set = set;
  659. attr->data = inode->i_private;
  660. attr->fmt = fmt;
  661. mutex_init(&attr->mutex);
  662. file->private_data = attr;
  663. return nonseekable_open(inode, file);
  664. }
  665. int simple_attr_release(struct inode *inode, struct file *file)
  666. {
  667. kfree(file->private_data);
  668. return 0;
  669. }
  670. /* read from the buffer that is filled with the get function */
  671. ssize_t simple_attr_read(struct file *file, char __user *buf,
  672. size_t len, loff_t *ppos)
  673. {
  674. struct simple_attr *attr;
  675. size_t size;
  676. ssize_t ret;
  677. attr = file->private_data;
  678. if (!attr->get)
  679. return -EACCES;
  680. ret = mutex_lock_interruptible(&attr->mutex);
  681. if (ret)
  682. return ret;
  683. if (*ppos) { /* continued read */
  684. size = strlen(attr->get_buf);
  685. } else { /* first read */
  686. u64 val;
  687. ret = attr->get(attr->data, &val);
  688. if (ret)
  689. goto out;
  690. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  691. attr->fmt, (unsigned long long)val);
  692. }
  693. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  694. out:
  695. mutex_unlock(&attr->mutex);
  696. return ret;
  697. }
  698. /* interpret the buffer as a number to call the set function with */
  699. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  700. size_t len, loff_t *ppos)
  701. {
  702. struct simple_attr *attr;
  703. u64 val;
  704. size_t size;
  705. ssize_t ret;
  706. attr = file->private_data;
  707. if (!attr->set)
  708. return -EACCES;
  709. ret = mutex_lock_interruptible(&attr->mutex);
  710. if (ret)
  711. return ret;
  712. ret = -EFAULT;
  713. size = min(sizeof(attr->set_buf) - 1, len);
  714. if (copy_from_user(attr->set_buf, buf, size))
  715. goto out;
  716. attr->set_buf[size] = '\0';
  717. val = simple_strtoll(attr->set_buf, NULL, 0);
  718. ret = attr->set(attr->data, val);
  719. if (ret == 0)
  720. ret = len; /* on success, claim we got the whole input */
  721. out:
  722. mutex_unlock(&attr->mutex);
  723. return ret;
  724. }
  725. /**
  726. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  727. * @sb: filesystem to do the file handle conversion on
  728. * @fid: file handle to convert
  729. * @fh_len: length of the file handle in bytes
  730. * @fh_type: type of file handle
  731. * @get_inode: filesystem callback to retrieve inode
  732. *
  733. * This function decodes @fid as long as it has one of the well-known
  734. * Linux filehandle types and calls @get_inode on it to retrieve the
  735. * inode for the object specified in the file handle.
  736. */
  737. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  738. int fh_len, int fh_type, struct inode *(*get_inode)
  739. (struct super_block *sb, u64 ino, u32 gen))
  740. {
  741. struct inode *inode = NULL;
  742. if (fh_len < 2)
  743. return NULL;
  744. switch (fh_type) {
  745. case FILEID_INO32_GEN:
  746. case FILEID_INO32_GEN_PARENT:
  747. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  748. break;
  749. }
  750. return d_obtain_alias(inode);
  751. }
  752. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  753. /**
  754. * generic_fh_to_dentry - generic helper for the fh_to_parent export operation
  755. * @sb: filesystem to do the file handle conversion on
  756. * @fid: file handle to convert
  757. * @fh_len: length of the file handle in bytes
  758. * @fh_type: type of file handle
  759. * @get_inode: filesystem callback to retrieve inode
  760. *
  761. * This function decodes @fid as long as it has one of the well-known
  762. * Linux filehandle types and calls @get_inode on it to retrieve the
  763. * inode for the _parent_ object specified in the file handle if it
  764. * is specified in the file handle, or NULL otherwise.
  765. */
  766. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  767. int fh_len, int fh_type, struct inode *(*get_inode)
  768. (struct super_block *sb, u64 ino, u32 gen))
  769. {
  770. struct inode *inode = NULL;
  771. if (fh_len <= 2)
  772. return NULL;
  773. switch (fh_type) {
  774. case FILEID_INO32_GEN_PARENT:
  775. inode = get_inode(sb, fid->i32.parent_ino,
  776. (fh_len > 3 ? fid->i32.parent_gen : 0));
  777. break;
  778. }
  779. return d_obtain_alias(inode);
  780. }
  781. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  782. /**
  783. * generic_file_fsync - generic fsync implementation for simple filesystems
  784. * @file: file to synchronize
  785. * @datasync: only synchronize essential metadata if true
  786. *
  787. * This is a generic implementation of the fsync method for simple
  788. * filesystems which track all non-inode metadata in the buffers list
  789. * hanging off the address_space structure.
  790. */
  791. int generic_file_fsync(struct file *file, loff_t start, loff_t end,
  792. int datasync)
  793. {
  794. struct inode *inode = file->f_mapping->host;
  795. int err;
  796. int ret;
  797. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  798. if (err)
  799. return err;
  800. mutex_lock(&inode->i_mutex);
  801. ret = sync_mapping_buffers(inode->i_mapping);
  802. if (!(inode->i_state & I_DIRTY))
  803. goto out;
  804. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  805. goto out;
  806. err = sync_inode_metadata(inode, 1);
  807. if (ret == 0)
  808. ret = err;
  809. out:
  810. mutex_unlock(&inode->i_mutex);
  811. return ret;
  812. }
  813. EXPORT_SYMBOL(generic_file_fsync);
  814. /**
  815. * generic_check_addressable - Check addressability of file system
  816. * @blocksize_bits: log of file system block size
  817. * @num_blocks: number of blocks in file system
  818. *
  819. * Determine whether a file system with @num_blocks blocks (and a
  820. * block size of 2**@blocksize_bits) is addressable by the sector_t
  821. * and page cache of the system. Return 0 if so and -EFBIG otherwise.
  822. */
  823. int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
  824. {
  825. u64 last_fs_block = num_blocks - 1;
  826. u64 last_fs_page =
  827. last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
  828. if (unlikely(num_blocks == 0))
  829. return 0;
  830. if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT))
  831. return -EINVAL;
  832. if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
  833. (last_fs_page > (pgoff_t)(~0ULL))) {
  834. return -EFBIG;
  835. }
  836. return 0;
  837. }
  838. EXPORT_SYMBOL(generic_check_addressable);
  839. /*
  840. * No-op implementation of ->fsync for in-memory filesystems.
  841. */
  842. int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  843. {
  844. return 0;
  845. }
  846. EXPORT_SYMBOL(dcache_dir_close);
  847. EXPORT_SYMBOL(dcache_dir_lseek);
  848. EXPORT_SYMBOL(dcache_dir_open);
  849. EXPORT_SYMBOL(dcache_readdir);
  850. EXPORT_SYMBOL(generic_read_dir);
  851. EXPORT_SYMBOL(mount_pseudo);
  852. EXPORT_SYMBOL(simple_write_begin);
  853. EXPORT_SYMBOL(simple_write_end);
  854. EXPORT_SYMBOL(simple_dir_inode_operations);
  855. EXPORT_SYMBOL(simple_dir_operations);
  856. EXPORT_SYMBOL(simple_empty);
  857. EXPORT_SYMBOL(simple_fill_super);
  858. EXPORT_SYMBOL(simple_getattr);
  859. EXPORT_SYMBOL(simple_link);
  860. EXPORT_SYMBOL(simple_lookup);
  861. EXPORT_SYMBOL(simple_pin_fs);
  862. EXPORT_SYMBOL(simple_readpage);
  863. EXPORT_SYMBOL(simple_release_fs);
  864. EXPORT_SYMBOL(simple_rename);
  865. EXPORT_SYMBOL(simple_rmdir);
  866. EXPORT_SYMBOL(simple_statfs);
  867. EXPORT_SYMBOL(noop_fsync);
  868. EXPORT_SYMBOL(simple_unlink);
  869. EXPORT_SYMBOL(simple_read_from_buffer);
  870. EXPORT_SYMBOL(simple_write_to_buffer);
  871. EXPORT_SYMBOL(memory_read_from_buffer);
  872. EXPORT_SYMBOL(simple_transaction_set);
  873. EXPORT_SYMBOL(simple_transaction_get);
  874. EXPORT_SYMBOL(simple_transaction_read);
  875. EXPORT_SYMBOL(simple_transaction_release);
  876. EXPORT_SYMBOL_GPL(simple_attr_open);
  877. EXPORT_SYMBOL_GPL(simple_attr_release);
  878. EXPORT_SYMBOL_GPL(simple_attr_read);
  879. EXPORT_SYMBOL_GPL(simple_attr_write);