libfs.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /*
  2. * fs/libfs.c
  3. * Library for filesystems writers.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/export.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/slab.h>
  9. #include <linux/cred.h>
  10. #include <linux/mount.h>
  11. #include <linux/vfs.h>
  12. #include <linux/quotaops.h>
  13. #include <linux/mutex.h>
  14. #include <linux/namei.h>
  15. #include <linux/exportfs.h>
  16. #include <linux/writeback.h>
  17. #include <linux/buffer_head.h> /* sync_mapping_buffers */
  18. #include <linux/uaccess.h>
  19. #include "internal.h"
  20. int simple_getattr(const struct path *path, struct kstat *stat,
  21. u32 request_mask, unsigned int query_flags)
  22. {
  23. struct inode *inode = d_inode(path->dentry);
  24. generic_fillattr(inode, stat);
  25. stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
  26. return 0;
  27. }
  28. EXPORT_SYMBOL(simple_getattr);
  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_SIZE;
  33. buf->f_namelen = NAME_MAX;
  34. return 0;
  35. }
  36. EXPORT_SYMBOL(simple_statfs);
  37. /*
  38. * Retaining negative dentries for an in-memory filesystem just wastes
  39. * memory and lookup time: arrange for them to be deleted immediately.
  40. */
  41. int always_delete_dentry(const struct dentry *dentry)
  42. {
  43. return 1;
  44. }
  45. EXPORT_SYMBOL(always_delete_dentry);
  46. const struct dentry_operations simple_dentry_operations = {
  47. .d_delete = always_delete_dentry,
  48. };
  49. EXPORT_SYMBOL(simple_dentry_operations);
  50. /*
  51. * Lookup the data. This is trivial - if the dentry didn't already
  52. * exist, we know it is negative. Set d_op to delete negative dentries.
  53. */
  54. struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  55. {
  56. if (dentry->d_name.len > NAME_MAX)
  57. return ERR_PTR(-ENAMETOOLONG);
  58. if (!dentry->d_sb->s_d_op)
  59. d_set_d_op(dentry, &simple_dentry_operations);
  60. d_add(dentry, NULL);
  61. return NULL;
  62. }
  63. EXPORT_SYMBOL(simple_lookup);
  64. int dcache_dir_open(struct inode *inode, struct file *file)
  65. {
  66. file->private_data = d_alloc_cursor(file->f_path.dentry);
  67. return file->private_data ? 0 : -ENOMEM;
  68. }
  69. EXPORT_SYMBOL(dcache_dir_open);
  70. int dcache_dir_close(struct inode *inode, struct file *file)
  71. {
  72. dput(file->private_data);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(dcache_dir_close);
  76. /* parent is locked at least shared */
  77. static struct dentry *next_positive(struct dentry *parent,
  78. struct list_head *from,
  79. int count)
  80. {
  81. unsigned *seq = &parent->d_inode->i_dir_seq, n;
  82. struct dentry *res;
  83. struct list_head *p;
  84. bool skipped;
  85. int i;
  86. retry:
  87. i = count;
  88. skipped = false;
  89. n = smp_load_acquire(seq) & ~1;
  90. res = NULL;
  91. rcu_read_lock();
  92. for (p = from->next; p != &parent->d_subdirs; p = p->next) {
  93. struct dentry *d = list_entry(p, struct dentry, d_child);
  94. if (!simple_positive(d)) {
  95. skipped = true;
  96. } else if (!--i) {
  97. res = d;
  98. break;
  99. }
  100. }
  101. rcu_read_unlock();
  102. if (skipped) {
  103. smp_rmb();
  104. if (unlikely(*seq != n))
  105. goto retry;
  106. }
  107. return res;
  108. }
  109. static void move_cursor(struct dentry *cursor, struct list_head *after)
  110. {
  111. struct dentry *parent = cursor->d_parent;
  112. unsigned n, *seq = &parent->d_inode->i_dir_seq;
  113. spin_lock(&parent->d_lock);
  114. for (;;) {
  115. n = *seq;
  116. if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
  117. break;
  118. cpu_relax();
  119. }
  120. __list_del(cursor->d_child.prev, cursor->d_child.next);
  121. if (after)
  122. list_add(&cursor->d_child, after);
  123. else
  124. list_add_tail(&cursor->d_child, &parent->d_subdirs);
  125. smp_store_release(seq, n + 2);
  126. spin_unlock(&parent->d_lock);
  127. }
  128. loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
  129. {
  130. struct dentry *dentry = file->f_path.dentry;
  131. switch (whence) {
  132. case 1:
  133. offset += file->f_pos;
  134. case 0:
  135. if (offset >= 0)
  136. break;
  137. default:
  138. return -EINVAL;
  139. }
  140. if (offset != file->f_pos) {
  141. file->f_pos = offset;
  142. if (file->f_pos >= 2) {
  143. struct dentry *cursor = file->private_data;
  144. struct dentry *to;
  145. loff_t n = file->f_pos - 2;
  146. inode_lock_shared(dentry->d_inode);
  147. to = next_positive(dentry, &dentry->d_subdirs, n);
  148. move_cursor(cursor, to ? &to->d_child : NULL);
  149. inode_unlock_shared(dentry->d_inode);
  150. }
  151. }
  152. return offset;
  153. }
  154. EXPORT_SYMBOL(dcache_dir_lseek);
  155. /* Relationship between i_mode and the DT_xxx types */
  156. static inline unsigned char dt_type(struct inode *inode)
  157. {
  158. return (inode->i_mode >> 12) & 15;
  159. }
  160. /*
  161. * Directory is locked and all positive dentries in it are safe, since
  162. * for ramfs-type trees they can't go away without unlink() or rmdir(),
  163. * both impossible due to the lock on directory.
  164. */
  165. int dcache_readdir(struct file *file, struct dir_context *ctx)
  166. {
  167. struct dentry *dentry = file->f_path.dentry;
  168. struct dentry *cursor = file->private_data;
  169. struct list_head *p = &cursor->d_child;
  170. struct dentry *next;
  171. bool moved = false;
  172. if (!dir_emit_dots(file, ctx))
  173. return 0;
  174. if (ctx->pos == 2)
  175. p = &dentry->d_subdirs;
  176. while ((next = next_positive(dentry, p, 1)) != NULL) {
  177. if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
  178. d_inode(next)->i_ino, dt_type(d_inode(next))))
  179. break;
  180. moved = true;
  181. p = &next->d_child;
  182. ctx->pos++;
  183. }
  184. if (moved)
  185. move_cursor(cursor, p);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(dcache_readdir);
  189. ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
  190. {
  191. return -EISDIR;
  192. }
  193. EXPORT_SYMBOL(generic_read_dir);
  194. const struct file_operations simple_dir_operations = {
  195. .open = dcache_dir_open,
  196. .release = dcache_dir_close,
  197. .llseek = dcache_dir_lseek,
  198. .read = generic_read_dir,
  199. .iterate_shared = dcache_readdir,
  200. .fsync = noop_fsync,
  201. };
  202. EXPORT_SYMBOL(simple_dir_operations);
  203. const struct inode_operations simple_dir_inode_operations = {
  204. .lookup = simple_lookup,
  205. };
  206. EXPORT_SYMBOL(simple_dir_inode_operations);
  207. static const struct super_operations simple_super_operations = {
  208. .statfs = simple_statfs,
  209. };
  210. /*
  211. * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
  212. * will never be mountable)
  213. */
  214. struct dentry *mount_pseudo_xattr(struct file_system_type *fs_type, char *name,
  215. const struct super_operations *ops, const struct xattr_handler **xattr,
  216. const struct dentry_operations *dops, unsigned long magic)
  217. {
  218. struct super_block *s;
  219. struct dentry *dentry;
  220. struct inode *root;
  221. struct qstr d_name = QSTR_INIT(name, strlen(name));
  222. s = sget_userns(fs_type, NULL, set_anon_super, SB_KERNMOUNT|SB_NOUSER,
  223. &init_user_ns, NULL);
  224. if (IS_ERR(s))
  225. return ERR_CAST(s);
  226. s->s_maxbytes = MAX_LFS_FILESIZE;
  227. s->s_blocksize = PAGE_SIZE;
  228. s->s_blocksize_bits = PAGE_SHIFT;
  229. s->s_magic = magic;
  230. s->s_op = ops ? ops : &simple_super_operations;
  231. s->s_xattr = xattr;
  232. s->s_time_gran = 1;
  233. root = new_inode(s);
  234. if (!root)
  235. goto Enomem;
  236. /*
  237. * since this is the first inode, make it number 1. New inodes created
  238. * after this must take care not to collide with it (by passing
  239. * max_reserved of 1 to iunique).
  240. */
  241. root->i_ino = 1;
  242. root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
  243. root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
  244. dentry = __d_alloc(s, &d_name);
  245. if (!dentry) {
  246. iput(root);
  247. goto Enomem;
  248. }
  249. d_instantiate(dentry, root);
  250. s->s_root = dentry;
  251. s->s_d_op = dops;
  252. s->s_flags |= SB_ACTIVE;
  253. return dget(s->s_root);
  254. Enomem:
  255. deactivate_locked_super(s);
  256. return ERR_PTR(-ENOMEM);
  257. }
  258. EXPORT_SYMBOL(mount_pseudo_xattr);
  259. int simple_open(struct inode *inode, struct file *file)
  260. {
  261. if (inode->i_private)
  262. file->private_data = inode->i_private;
  263. return 0;
  264. }
  265. EXPORT_SYMBOL(simple_open);
  266. int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  267. {
  268. struct inode *inode = d_inode(old_dentry);
  269. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  270. inc_nlink(inode);
  271. ihold(inode);
  272. dget(dentry);
  273. d_instantiate(dentry, inode);
  274. return 0;
  275. }
  276. EXPORT_SYMBOL(simple_link);
  277. int simple_empty(struct dentry *dentry)
  278. {
  279. struct dentry *child;
  280. int ret = 0;
  281. spin_lock(&dentry->d_lock);
  282. list_for_each_entry(child, &dentry->d_subdirs, d_child) {
  283. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  284. if (simple_positive(child)) {
  285. spin_unlock(&child->d_lock);
  286. goto out;
  287. }
  288. spin_unlock(&child->d_lock);
  289. }
  290. ret = 1;
  291. out:
  292. spin_unlock(&dentry->d_lock);
  293. return ret;
  294. }
  295. EXPORT_SYMBOL(simple_empty);
  296. int simple_unlink(struct inode *dir, struct dentry *dentry)
  297. {
  298. struct inode *inode = d_inode(dentry);
  299. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  300. drop_nlink(inode);
  301. dput(dentry);
  302. return 0;
  303. }
  304. EXPORT_SYMBOL(simple_unlink);
  305. int simple_rmdir(struct inode *dir, struct dentry *dentry)
  306. {
  307. if (!simple_empty(dentry))
  308. return -ENOTEMPTY;
  309. drop_nlink(d_inode(dentry));
  310. simple_unlink(dir, dentry);
  311. drop_nlink(dir);
  312. return 0;
  313. }
  314. EXPORT_SYMBOL(simple_rmdir);
  315. int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
  316. struct inode *new_dir, struct dentry *new_dentry,
  317. unsigned int flags)
  318. {
  319. struct inode *inode = d_inode(old_dentry);
  320. int they_are_dirs = d_is_dir(old_dentry);
  321. if (flags & ~RENAME_NOREPLACE)
  322. return -EINVAL;
  323. if (!simple_empty(new_dentry))
  324. return -ENOTEMPTY;
  325. if (d_really_is_positive(new_dentry)) {
  326. simple_unlink(new_dir, new_dentry);
  327. if (they_are_dirs) {
  328. drop_nlink(d_inode(new_dentry));
  329. drop_nlink(old_dir);
  330. }
  331. } else if (they_are_dirs) {
  332. drop_nlink(old_dir);
  333. inc_nlink(new_dir);
  334. }
  335. old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
  336. new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
  337. return 0;
  338. }
  339. EXPORT_SYMBOL(simple_rename);
  340. /**
  341. * simple_setattr - setattr for simple filesystem
  342. * @dentry: dentry
  343. * @iattr: iattr structure
  344. *
  345. * Returns 0 on success, -error on failure.
  346. *
  347. * simple_setattr is a simple ->setattr implementation without a proper
  348. * implementation of size changes.
  349. *
  350. * It can either be used for in-memory filesystems or special files
  351. * on simple regular filesystems. Anything that needs to change on-disk
  352. * or wire state on size changes needs its own setattr method.
  353. */
  354. int simple_setattr(struct dentry *dentry, struct iattr *iattr)
  355. {
  356. struct inode *inode = d_inode(dentry);
  357. int error;
  358. error = setattr_prepare(dentry, iattr);
  359. if (error)
  360. return error;
  361. if (iattr->ia_valid & ATTR_SIZE)
  362. truncate_setsize(inode, iattr->ia_size);
  363. setattr_copy(inode, iattr);
  364. mark_inode_dirty(inode);
  365. return 0;
  366. }
  367. EXPORT_SYMBOL(simple_setattr);
  368. int simple_readpage(struct file *file, struct page *page)
  369. {
  370. clear_highpage(page);
  371. flush_dcache_page(page);
  372. SetPageUptodate(page);
  373. unlock_page(page);
  374. return 0;
  375. }
  376. EXPORT_SYMBOL(simple_readpage);
  377. int simple_write_begin(struct file *file, struct address_space *mapping,
  378. loff_t pos, unsigned len, unsigned flags,
  379. struct page **pagep, void **fsdata)
  380. {
  381. struct page *page;
  382. pgoff_t index;
  383. index = pos >> PAGE_SHIFT;
  384. page = grab_cache_page_write_begin(mapping, index, flags);
  385. if (!page)
  386. return -ENOMEM;
  387. *pagep = page;
  388. if (!PageUptodate(page) && (len != PAGE_SIZE)) {
  389. unsigned from = pos & (PAGE_SIZE - 1);
  390. zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
  391. }
  392. return 0;
  393. }
  394. EXPORT_SYMBOL(simple_write_begin);
  395. /**
  396. * simple_write_end - .write_end helper for non-block-device FSes
  397. * @available: See .write_end of address_space_operations
  398. * @file: "
  399. * @mapping: "
  400. * @pos: "
  401. * @len: "
  402. * @copied: "
  403. * @page: "
  404. * @fsdata: "
  405. *
  406. * simple_write_end does the minimum needed for updating a page after writing is
  407. * done. It has the same API signature as the .write_end of
  408. * address_space_operations vector. So it can just be set onto .write_end for
  409. * FSes that don't need any other processing. i_mutex is assumed to be held.
  410. * Block based filesystems should use generic_write_end().
  411. * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
  412. * is not called, so a filesystem that actually does store data in .write_inode
  413. * should extend on what's done here with a call to mark_inode_dirty() in the
  414. * case that i_size has changed.
  415. *
  416. * Use *ONLY* with simple_readpage()
  417. */
  418. int simple_write_end(struct file *file, struct address_space *mapping,
  419. loff_t pos, unsigned len, unsigned copied,
  420. struct page *page, void *fsdata)
  421. {
  422. struct inode *inode = page->mapping->host;
  423. loff_t last_pos = pos + copied;
  424. /* zero the stale part of the page if we did a short copy */
  425. if (!PageUptodate(page)) {
  426. if (copied < len) {
  427. unsigned from = pos & (PAGE_SIZE - 1);
  428. zero_user(page, from + copied, len - copied);
  429. }
  430. SetPageUptodate(page);
  431. }
  432. /*
  433. * No need to use i_size_read() here, the i_size
  434. * cannot change under us because we hold the i_mutex.
  435. */
  436. if (last_pos > inode->i_size)
  437. i_size_write(inode, last_pos);
  438. set_page_dirty(page);
  439. unlock_page(page);
  440. put_page(page);
  441. return copied;
  442. }
  443. EXPORT_SYMBOL(simple_write_end);
  444. /*
  445. * the inodes created here are not hashed. If you use iunique to generate
  446. * unique inode values later for this filesystem, then you must take care
  447. * to pass it an appropriate max_reserved value to avoid collisions.
  448. */
  449. int simple_fill_super(struct super_block *s, unsigned long magic,
  450. const struct tree_descr *files)
  451. {
  452. struct inode *inode;
  453. struct dentry *root;
  454. struct dentry *dentry;
  455. int i;
  456. s->s_blocksize = PAGE_SIZE;
  457. s->s_blocksize_bits = PAGE_SHIFT;
  458. s->s_magic = magic;
  459. s->s_op = &simple_super_operations;
  460. s->s_time_gran = 1;
  461. inode = new_inode(s);
  462. if (!inode)
  463. return -ENOMEM;
  464. /*
  465. * because the root inode is 1, the files array must not contain an
  466. * entry at index 1
  467. */
  468. inode->i_ino = 1;
  469. inode->i_mode = S_IFDIR | 0755;
  470. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  471. inode->i_op = &simple_dir_inode_operations;
  472. inode->i_fop = &simple_dir_operations;
  473. set_nlink(inode, 2);
  474. root = d_make_root(inode);
  475. if (!root)
  476. return -ENOMEM;
  477. for (i = 0; !files->name || files->name[0]; i++, files++) {
  478. if (!files->name)
  479. continue;
  480. /* warn if it tries to conflict with the root inode */
  481. if (unlikely(i == 1))
  482. printk(KERN_WARNING "%s: %s passed in a files array"
  483. "with an index of 1!\n", __func__,
  484. s->s_type->name);
  485. dentry = d_alloc_name(root, files->name);
  486. if (!dentry)
  487. goto out;
  488. inode = new_inode(s);
  489. if (!inode) {
  490. dput(dentry);
  491. goto out;
  492. }
  493. inode->i_mode = S_IFREG | files->mode;
  494. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  495. inode->i_fop = files->ops;
  496. inode->i_ino = i;
  497. d_add(dentry, inode);
  498. }
  499. s->s_root = root;
  500. return 0;
  501. out:
  502. d_genocide(root);
  503. shrink_dcache_parent(root);
  504. dput(root);
  505. return -ENOMEM;
  506. }
  507. EXPORT_SYMBOL(simple_fill_super);
  508. static DEFINE_SPINLOCK(pin_fs_lock);
  509. int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
  510. {
  511. struct vfsmount *mnt = NULL;
  512. spin_lock(&pin_fs_lock);
  513. if (unlikely(!*mount)) {
  514. spin_unlock(&pin_fs_lock);
  515. mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
  516. if (IS_ERR(mnt))
  517. return PTR_ERR(mnt);
  518. spin_lock(&pin_fs_lock);
  519. if (!*mount)
  520. *mount = mnt;
  521. }
  522. mntget(*mount);
  523. ++*count;
  524. spin_unlock(&pin_fs_lock);
  525. mntput(mnt);
  526. return 0;
  527. }
  528. EXPORT_SYMBOL(simple_pin_fs);
  529. void simple_release_fs(struct vfsmount **mount, int *count)
  530. {
  531. struct vfsmount *mnt;
  532. spin_lock(&pin_fs_lock);
  533. mnt = *mount;
  534. if (!--*count)
  535. *mount = NULL;
  536. spin_unlock(&pin_fs_lock);
  537. mntput(mnt);
  538. }
  539. EXPORT_SYMBOL(simple_release_fs);
  540. /**
  541. * simple_read_from_buffer - copy data from the buffer to user space
  542. * @to: the user space buffer to read to
  543. * @count: the maximum number of bytes to read
  544. * @ppos: the current position in the buffer
  545. * @from: the buffer to read from
  546. * @available: the size of the buffer
  547. *
  548. * The simple_read_from_buffer() function reads up to @count bytes from the
  549. * buffer @from at offset @ppos into the user space address starting at @to.
  550. *
  551. * On success, the number of bytes read is returned and the offset @ppos is
  552. * advanced by this number, or negative value is returned on error.
  553. **/
  554. ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
  555. const void *from, size_t available)
  556. {
  557. loff_t pos = *ppos;
  558. size_t ret;
  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. ret = copy_to_user(to, from + pos, count);
  566. if (ret == count)
  567. return -EFAULT;
  568. count -= ret;
  569. *ppos = pos + count;
  570. return count;
  571. }
  572. EXPORT_SYMBOL(simple_read_from_buffer);
  573. /**
  574. * simple_write_to_buffer - copy data from user space to the buffer
  575. * @to: the buffer to write to
  576. * @available: the size of the buffer
  577. * @ppos: the current position in the buffer
  578. * @from: the user space buffer to read from
  579. * @count: the maximum number of bytes to read
  580. *
  581. * The simple_write_to_buffer() function reads up to @count bytes from the user
  582. * space address starting at @from into the buffer @to at offset @ppos.
  583. *
  584. * On success, the number of bytes written is returned and the offset @ppos is
  585. * advanced by this number, or negative value is returned on error.
  586. **/
  587. ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  588. const void __user *from, size_t count)
  589. {
  590. loff_t pos = *ppos;
  591. size_t res;
  592. if (pos < 0)
  593. return -EINVAL;
  594. if (pos >= available || !count)
  595. return 0;
  596. if (count > available - pos)
  597. count = available - pos;
  598. res = copy_from_user(to + pos, from, count);
  599. if (res == count)
  600. return -EFAULT;
  601. count -= res;
  602. *ppos = pos + count;
  603. return count;
  604. }
  605. EXPORT_SYMBOL(simple_write_to_buffer);
  606. /**
  607. * memory_read_from_buffer - copy data from the buffer
  608. * @to: the kernel space buffer to read to
  609. * @count: the maximum number of bytes to read
  610. * @ppos: the current position in the buffer
  611. * @from: the buffer to read from
  612. * @available: the size of the buffer
  613. *
  614. * The memory_read_from_buffer() function reads up to @count bytes from the
  615. * buffer @from at offset @ppos into the kernel space address starting at @to.
  616. *
  617. * On success, the number of bytes read is returned and the offset @ppos is
  618. * advanced by this number, or negative value is returned on error.
  619. **/
  620. ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  621. const void *from, size_t available)
  622. {
  623. loff_t pos = *ppos;
  624. if (pos < 0)
  625. return -EINVAL;
  626. if (pos >= available)
  627. return 0;
  628. if (count > available - pos)
  629. count = available - pos;
  630. memcpy(to, from + pos, count);
  631. *ppos = pos + count;
  632. return count;
  633. }
  634. EXPORT_SYMBOL(memory_read_from_buffer);
  635. /*
  636. * Transaction based IO.
  637. * The file expects a single write which triggers the transaction, and then
  638. * possibly a read which collects the result - which is stored in a
  639. * file-local buffer.
  640. */
  641. void simple_transaction_set(struct file *file, size_t n)
  642. {
  643. struct simple_transaction_argresp *ar = file->private_data;
  644. BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
  645. /*
  646. * The barrier ensures that ar->size will really remain zero until
  647. * ar->data is ready for reading.
  648. */
  649. smp_mb();
  650. ar->size = n;
  651. }
  652. EXPORT_SYMBOL(simple_transaction_set);
  653. char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
  654. {
  655. struct simple_transaction_argresp *ar;
  656. static DEFINE_SPINLOCK(simple_transaction_lock);
  657. if (size > SIMPLE_TRANSACTION_LIMIT - 1)
  658. return ERR_PTR(-EFBIG);
  659. ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
  660. if (!ar)
  661. return ERR_PTR(-ENOMEM);
  662. spin_lock(&simple_transaction_lock);
  663. /* only one write allowed per open */
  664. if (file->private_data) {
  665. spin_unlock(&simple_transaction_lock);
  666. free_page((unsigned long)ar);
  667. return ERR_PTR(-EBUSY);
  668. }
  669. file->private_data = ar;
  670. spin_unlock(&simple_transaction_lock);
  671. if (copy_from_user(ar->data, buf, size))
  672. return ERR_PTR(-EFAULT);
  673. return ar->data;
  674. }
  675. EXPORT_SYMBOL(simple_transaction_get);
  676. ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
  677. {
  678. struct simple_transaction_argresp *ar = file->private_data;
  679. if (!ar)
  680. return 0;
  681. return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
  682. }
  683. EXPORT_SYMBOL(simple_transaction_read);
  684. int simple_transaction_release(struct inode *inode, struct file *file)
  685. {
  686. free_page((unsigned long)file->private_data);
  687. return 0;
  688. }
  689. EXPORT_SYMBOL(simple_transaction_release);
  690. /* Simple attribute files */
  691. struct simple_attr {
  692. int (*get)(void *, u64 *);
  693. int (*set)(void *, u64);
  694. char get_buf[24]; /* enough to store a u64 and "\n\0" */
  695. char set_buf[24];
  696. void *data;
  697. const char *fmt; /* format for read operation */
  698. struct mutex mutex; /* protects access to these buffers */
  699. };
  700. /* simple_attr_open is called by an actual attribute open file operation
  701. * to set the attribute specific access operations. */
  702. int simple_attr_open(struct inode *inode, struct file *file,
  703. int (*get)(void *, u64 *), int (*set)(void *, u64),
  704. const char *fmt)
  705. {
  706. struct simple_attr *attr;
  707. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  708. if (!attr)
  709. return -ENOMEM;
  710. attr->get = get;
  711. attr->set = set;
  712. attr->data = inode->i_private;
  713. attr->fmt = fmt;
  714. mutex_init(&attr->mutex);
  715. file->private_data = attr;
  716. return nonseekable_open(inode, file);
  717. }
  718. EXPORT_SYMBOL_GPL(simple_attr_open);
  719. int simple_attr_release(struct inode *inode, struct file *file)
  720. {
  721. kfree(file->private_data);
  722. return 0;
  723. }
  724. EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
  725. /* read from the buffer that is filled with the get function */
  726. ssize_t simple_attr_read(struct file *file, char __user *buf,
  727. size_t len, loff_t *ppos)
  728. {
  729. struct simple_attr *attr;
  730. size_t size;
  731. ssize_t ret;
  732. attr = file->private_data;
  733. if (!attr->get)
  734. return -EACCES;
  735. ret = mutex_lock_interruptible(&attr->mutex);
  736. if (ret)
  737. return ret;
  738. if (*ppos) { /* continued read */
  739. size = strlen(attr->get_buf);
  740. } else { /* first read */
  741. u64 val;
  742. ret = attr->get(attr->data, &val);
  743. if (ret)
  744. goto out;
  745. size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
  746. attr->fmt, (unsigned long long)val);
  747. }
  748. ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
  749. out:
  750. mutex_unlock(&attr->mutex);
  751. return ret;
  752. }
  753. EXPORT_SYMBOL_GPL(simple_attr_read);
  754. /* interpret the buffer as a number to call the set function with */
  755. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  756. size_t len, loff_t *ppos)
  757. {
  758. struct simple_attr *attr;
  759. u64 val;
  760. size_t size;
  761. ssize_t ret;
  762. attr = file->private_data;
  763. if (!attr->set)
  764. return -EACCES;
  765. ret = mutex_lock_interruptible(&attr->mutex);
  766. if (ret)
  767. return ret;
  768. ret = -EFAULT;
  769. size = min(sizeof(attr->set_buf) - 1, len);
  770. if (copy_from_user(attr->set_buf, buf, size))
  771. goto out;
  772. attr->set_buf[size] = '\0';
  773. val = simple_strtoll(attr->set_buf, NULL, 0);
  774. ret = attr->set(attr->data, val);
  775. if (ret == 0)
  776. ret = len; /* on success, claim we got the whole input */
  777. out:
  778. mutex_unlock(&attr->mutex);
  779. return ret;
  780. }
  781. EXPORT_SYMBOL_GPL(simple_attr_write);
  782. /**
  783. * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
  784. * @sb: filesystem to do the file handle conversion on
  785. * @fid: file handle to convert
  786. * @fh_len: length of the file handle in bytes
  787. * @fh_type: type of file handle
  788. * @get_inode: filesystem callback to retrieve inode
  789. *
  790. * This function decodes @fid as long as it has one of the well-known
  791. * Linux filehandle types and calls @get_inode on it to retrieve the
  792. * inode for the object specified in the file handle.
  793. */
  794. struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
  795. int fh_len, int fh_type, struct inode *(*get_inode)
  796. (struct super_block *sb, u64 ino, u32 gen))
  797. {
  798. struct inode *inode = NULL;
  799. if (fh_len < 2)
  800. return NULL;
  801. switch (fh_type) {
  802. case FILEID_INO32_GEN:
  803. case FILEID_INO32_GEN_PARENT:
  804. inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
  805. break;
  806. }
  807. return d_obtain_alias(inode);
  808. }
  809. EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
  810. /**
  811. * generic_fh_to_parent - generic helper for the fh_to_parent export operation
  812. * @sb: filesystem to do the file handle conversion on
  813. * @fid: file handle to convert
  814. * @fh_len: length of the file handle in bytes
  815. * @fh_type: type of file handle
  816. * @get_inode: filesystem callback to retrieve inode
  817. *
  818. * This function decodes @fid as long as it has one of the well-known
  819. * Linux filehandle types and calls @get_inode on it to retrieve the
  820. * inode for the _parent_ object specified in the file handle if it
  821. * is specified in the file handle, or NULL otherwise.
  822. */
  823. struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
  824. int fh_len, int fh_type, struct inode *(*get_inode)
  825. (struct super_block *sb, u64 ino, u32 gen))
  826. {
  827. struct inode *inode = NULL;
  828. if (fh_len <= 2)
  829. return NULL;
  830. switch (fh_type) {
  831. case FILEID_INO32_GEN_PARENT:
  832. inode = get_inode(sb, fid->i32.parent_ino,
  833. (fh_len > 3 ? fid->i32.parent_gen : 0));
  834. break;
  835. }
  836. return d_obtain_alias(inode);
  837. }
  838. EXPORT_SYMBOL_GPL(generic_fh_to_parent);
  839. /**
  840. * __generic_file_fsync - generic fsync implementation for simple filesystems
  841. *
  842. * @file: file to synchronize
  843. * @start: start offset in bytes
  844. * @end: end offset in bytes (inclusive)
  845. * @datasync: only synchronize essential metadata if true
  846. *
  847. * This is a generic implementation of the fsync method for simple
  848. * filesystems which track all non-inode metadata in the buffers list
  849. * hanging off the address_space structure.
  850. */
  851. int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
  852. int datasync)
  853. {
  854. struct inode *inode = file->f_mapping->host;
  855. int err;
  856. int ret;
  857. err = file_write_and_wait_range(file, start, end);
  858. if (err)
  859. return err;
  860. inode_lock(inode);
  861. ret = sync_mapping_buffers(inode->i_mapping);
  862. if (!(inode->i_state & I_DIRTY_ALL))
  863. goto out;
  864. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
  865. goto out;
  866. err = sync_inode_metadata(inode, 1);
  867. if (ret == 0)
  868. ret = err;
  869. out:
  870. inode_unlock(inode);
  871. /* check and advance again to catch errors after syncing out buffers */
  872. err = file_check_and_advance_wb_err(file);
  873. if (ret == 0)
  874. ret = err;
  875. return ret;
  876. }
  877. EXPORT_SYMBOL(__generic_file_fsync);
  878. /**
  879. * generic_file_fsync - generic fsync implementation for simple filesystems
  880. * with flush
  881. * @file: file to synchronize
  882. * @start: start offset in bytes
  883. * @end: end offset in bytes (inclusive)
  884. * @datasync: only synchronize essential metadata if true
  885. *
  886. */
  887. int generic_file_fsync(struct file *file, loff_t start, loff_t end,
  888. int datasync)
  889. {
  890. struct inode *inode = file->f_mapping->host;
  891. int err;
  892. err = __generic_file_fsync(file, start, end, datasync);
  893. if (err)
  894. return err;
  895. return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  896. }
  897. EXPORT_SYMBOL(generic_file_fsync);
  898. /**
  899. * generic_check_addressable - Check addressability of file system
  900. * @blocksize_bits: log of file system block size
  901. * @num_blocks: number of blocks in file system
  902. *
  903. * Determine whether a file system with @num_blocks blocks (and a
  904. * block size of 2**@blocksize_bits) is addressable by the sector_t
  905. * and page cache of the system. Return 0 if so and -EFBIG otherwise.
  906. */
  907. int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
  908. {
  909. u64 last_fs_block = num_blocks - 1;
  910. u64 last_fs_page =
  911. last_fs_block >> (PAGE_SHIFT - blocksize_bits);
  912. if (unlikely(num_blocks == 0))
  913. return 0;
  914. if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
  915. return -EINVAL;
  916. if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
  917. (last_fs_page > (pgoff_t)(~0ULL))) {
  918. return -EFBIG;
  919. }
  920. return 0;
  921. }
  922. EXPORT_SYMBOL(generic_check_addressable);
  923. /*
  924. * No-op implementation of ->fsync for in-memory filesystems.
  925. */
  926. int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  927. {
  928. return 0;
  929. }
  930. EXPORT_SYMBOL(noop_fsync);
  931. int noop_set_page_dirty(struct page *page)
  932. {
  933. /*
  934. * Unlike __set_page_dirty_no_writeback that handles dirty page
  935. * tracking in the page object, dax does all dirty tracking in
  936. * the inode address_space in response to mkwrite faults. In the
  937. * dax case we only need to worry about potentially dirty CPU
  938. * caches, not dirty page cache pages to write back.
  939. *
  940. * This callback is defined to prevent fallback to
  941. * __set_page_dirty_buffers() in set_page_dirty().
  942. */
  943. return 0;
  944. }
  945. EXPORT_SYMBOL_GPL(noop_set_page_dirty);
  946. void noop_invalidatepage(struct page *page, unsigned int offset,
  947. unsigned int length)
  948. {
  949. /*
  950. * There is no page cache to invalidate in the dax case, however
  951. * we need this callback defined to prevent falling back to
  952. * block_invalidatepage() in do_invalidatepage().
  953. */
  954. }
  955. EXPORT_SYMBOL_GPL(noop_invalidatepage);
  956. ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  957. {
  958. /*
  959. * iomap based filesystems support direct I/O without need for
  960. * this callback. However, it still needs to be set in
  961. * inode->a_ops so that open/fcntl know that direct I/O is
  962. * generally supported.
  963. */
  964. return -EINVAL;
  965. }
  966. EXPORT_SYMBOL_GPL(noop_direct_IO);
  967. /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
  968. void kfree_link(void *p)
  969. {
  970. kfree(p);
  971. }
  972. EXPORT_SYMBOL(kfree_link);
  973. /*
  974. * nop .set_page_dirty method so that people can use .page_mkwrite on
  975. * anon inodes.
  976. */
  977. static int anon_set_page_dirty(struct page *page)
  978. {
  979. return 0;
  980. };
  981. /*
  982. * A single inode exists for all anon_inode files. Contrary to pipes,
  983. * anon_inode inodes have no associated per-instance data, so we need
  984. * only allocate one of them.
  985. */
  986. struct inode *alloc_anon_inode(struct super_block *s)
  987. {
  988. static const struct address_space_operations anon_aops = {
  989. .set_page_dirty = anon_set_page_dirty,
  990. };
  991. struct inode *inode = new_inode_pseudo(s);
  992. if (!inode)
  993. return ERR_PTR(-ENOMEM);
  994. inode->i_ino = get_next_ino();
  995. inode->i_mapping->a_ops = &anon_aops;
  996. /*
  997. * Mark the inode dirty from the very beginning,
  998. * that way it will never be moved to the dirty
  999. * list because mark_inode_dirty() will think
  1000. * that it already _is_ on the dirty list.
  1001. */
  1002. inode->i_state = I_DIRTY;
  1003. inode->i_mode = S_IRUSR | S_IWUSR;
  1004. inode->i_uid = current_fsuid();
  1005. inode->i_gid = current_fsgid();
  1006. inode->i_flags |= S_PRIVATE;
  1007. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  1008. return inode;
  1009. }
  1010. EXPORT_SYMBOL(alloc_anon_inode);
  1011. /**
  1012. * simple_nosetlease - generic helper for prohibiting leases
  1013. * @filp: file pointer
  1014. * @arg: type of lease to obtain
  1015. * @flp: new lease supplied for insertion
  1016. * @priv: private data for lm_setup operation
  1017. *
  1018. * Generic helper for filesystems that do not wish to allow leases to be set.
  1019. * All arguments are ignored and it just returns -EINVAL.
  1020. */
  1021. int
  1022. simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
  1023. void **priv)
  1024. {
  1025. return -EINVAL;
  1026. }
  1027. EXPORT_SYMBOL(simple_nosetlease);
  1028. const char *simple_get_link(struct dentry *dentry, struct inode *inode,
  1029. struct delayed_call *done)
  1030. {
  1031. return inode->i_link;
  1032. }
  1033. EXPORT_SYMBOL(simple_get_link);
  1034. const struct inode_operations simple_symlink_inode_operations = {
  1035. .get_link = simple_get_link,
  1036. };
  1037. EXPORT_SYMBOL(simple_symlink_inode_operations);
  1038. /*
  1039. * Operations for a permanently empty directory.
  1040. */
  1041. static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1042. {
  1043. return ERR_PTR(-ENOENT);
  1044. }
  1045. static int empty_dir_getattr(const struct path *path, struct kstat *stat,
  1046. u32 request_mask, unsigned int query_flags)
  1047. {
  1048. struct inode *inode = d_inode(path->dentry);
  1049. generic_fillattr(inode, stat);
  1050. return 0;
  1051. }
  1052. static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
  1053. {
  1054. return -EPERM;
  1055. }
  1056. static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
  1057. {
  1058. return -EOPNOTSUPP;
  1059. }
  1060. static const struct inode_operations empty_dir_inode_operations = {
  1061. .lookup = empty_dir_lookup,
  1062. .permission = generic_permission,
  1063. .setattr = empty_dir_setattr,
  1064. .getattr = empty_dir_getattr,
  1065. .listxattr = empty_dir_listxattr,
  1066. };
  1067. static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
  1068. {
  1069. /* An empty directory has two entries . and .. at offsets 0 and 1 */
  1070. return generic_file_llseek_size(file, offset, whence, 2, 2);
  1071. }
  1072. static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
  1073. {
  1074. dir_emit_dots(file, ctx);
  1075. return 0;
  1076. }
  1077. static const struct file_operations empty_dir_operations = {
  1078. .llseek = empty_dir_llseek,
  1079. .read = generic_read_dir,
  1080. .iterate_shared = empty_dir_readdir,
  1081. .fsync = noop_fsync,
  1082. };
  1083. void make_empty_dir_inode(struct inode *inode)
  1084. {
  1085. set_nlink(inode, 2);
  1086. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  1087. inode->i_uid = GLOBAL_ROOT_UID;
  1088. inode->i_gid = GLOBAL_ROOT_GID;
  1089. inode->i_rdev = 0;
  1090. inode->i_size = 0;
  1091. inode->i_blkbits = PAGE_SHIFT;
  1092. inode->i_blocks = 0;
  1093. inode->i_op = &empty_dir_inode_operations;
  1094. inode->i_opflags &= ~IOP_XATTR;
  1095. inode->i_fop = &empty_dir_operations;
  1096. }
  1097. bool is_empty_dir_inode(struct inode *inode)
  1098. {
  1099. return (inode->i_fop == &empty_dir_operations) &&
  1100. (inode->i_op == &empty_dir_inode_operations);
  1101. }