libfs.c 26 KB

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