libfs.c 27 KB

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