inode.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/pagemap.h>
  9. #include <linux/slab.h>
  10. #include <linux/file.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/parser.h>
  15. #include <linux/statfs.h>
  16. #include <linux/random.h>
  17. #include <linux/sched.h>
  18. #include <linux/exportfs.h>
  19. MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
  20. MODULE_DESCRIPTION("Filesystem in Userspace");
  21. MODULE_LICENSE("GPL");
  22. static struct kmem_cache *fuse_inode_cachep;
  23. struct list_head fuse_conn_list;
  24. DEFINE_MUTEX(fuse_mutex);
  25. #define FUSE_SUPER_MAGIC 0x65735546
  26. #define FUSE_DEFAULT_BLKSIZE 512
  27. struct fuse_mount_data {
  28. int fd;
  29. unsigned rootmode;
  30. unsigned user_id;
  31. unsigned group_id;
  32. unsigned fd_present:1;
  33. unsigned rootmode_present:1;
  34. unsigned user_id_present:1;
  35. unsigned group_id_present:1;
  36. unsigned flags;
  37. unsigned max_read;
  38. unsigned blksize;
  39. };
  40. static struct inode *fuse_alloc_inode(struct super_block *sb)
  41. {
  42. struct inode *inode;
  43. struct fuse_inode *fi;
  44. inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
  45. if (!inode)
  46. return NULL;
  47. fi = get_fuse_inode(inode);
  48. fi->i_time = 0;
  49. fi->nodeid = 0;
  50. fi->nlookup = 0;
  51. fi->attr_version = 0;
  52. fi->writectr = 0;
  53. INIT_LIST_HEAD(&fi->write_files);
  54. INIT_LIST_HEAD(&fi->queued_writes);
  55. INIT_LIST_HEAD(&fi->writepages);
  56. init_waitqueue_head(&fi->page_waitq);
  57. fi->forget_req = fuse_request_alloc();
  58. if (!fi->forget_req) {
  59. kmem_cache_free(fuse_inode_cachep, inode);
  60. return NULL;
  61. }
  62. return inode;
  63. }
  64. static void fuse_destroy_inode(struct inode *inode)
  65. {
  66. struct fuse_inode *fi = get_fuse_inode(inode);
  67. BUG_ON(!list_empty(&fi->write_files));
  68. BUG_ON(!list_empty(&fi->queued_writes));
  69. if (fi->forget_req)
  70. fuse_request_free(fi->forget_req);
  71. kmem_cache_free(fuse_inode_cachep, inode);
  72. }
  73. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  74. u64 nodeid, u64 nlookup)
  75. {
  76. struct fuse_forget_in *inarg = &req->misc.forget_in;
  77. inarg->nlookup = nlookup;
  78. req->in.h.opcode = FUSE_FORGET;
  79. req->in.h.nodeid = nodeid;
  80. req->in.numargs = 1;
  81. req->in.args[0].size = sizeof(struct fuse_forget_in);
  82. req->in.args[0].value = inarg;
  83. fuse_request_send_noreply(fc, req);
  84. }
  85. static void fuse_clear_inode(struct inode *inode)
  86. {
  87. if (inode->i_sb->s_flags & MS_ACTIVE) {
  88. struct fuse_conn *fc = get_fuse_conn(inode);
  89. struct fuse_inode *fi = get_fuse_inode(inode);
  90. fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
  91. fi->forget_req = NULL;
  92. }
  93. }
  94. static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
  95. {
  96. if (*flags & MS_MANDLOCK)
  97. return -EINVAL;
  98. return 0;
  99. }
  100. void fuse_truncate(struct address_space *mapping, loff_t offset)
  101. {
  102. /* See vmtruncate() */
  103. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  104. truncate_inode_pages(mapping, offset);
  105. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  106. }
  107. void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
  108. u64 attr_valid)
  109. {
  110. struct fuse_conn *fc = get_fuse_conn(inode);
  111. struct fuse_inode *fi = get_fuse_inode(inode);
  112. fi->attr_version = ++fc->attr_version;
  113. fi->i_time = attr_valid;
  114. inode->i_ino = attr->ino;
  115. inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
  116. inode->i_nlink = attr->nlink;
  117. inode->i_uid = attr->uid;
  118. inode->i_gid = attr->gid;
  119. inode->i_blocks = attr->blocks;
  120. inode->i_atime.tv_sec = attr->atime;
  121. inode->i_atime.tv_nsec = attr->atimensec;
  122. inode->i_mtime.tv_sec = attr->mtime;
  123. inode->i_mtime.tv_nsec = attr->mtimensec;
  124. inode->i_ctime.tv_sec = attr->ctime;
  125. inode->i_ctime.tv_nsec = attr->ctimensec;
  126. if (attr->blksize != 0)
  127. inode->i_blkbits = ilog2(attr->blksize);
  128. else
  129. inode->i_blkbits = inode->i_sb->s_blocksize_bits;
  130. /*
  131. * Don't set the sticky bit in i_mode, unless we want the VFS
  132. * to check permissions. This prevents failures due to the
  133. * check in may_delete().
  134. */
  135. fi->orig_i_mode = inode->i_mode;
  136. if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
  137. inode->i_mode &= ~S_ISVTX;
  138. }
  139. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
  140. u64 attr_valid, u64 attr_version)
  141. {
  142. struct fuse_conn *fc = get_fuse_conn(inode);
  143. struct fuse_inode *fi = get_fuse_inode(inode);
  144. loff_t oldsize;
  145. spin_lock(&fc->lock);
  146. if (attr_version != 0 && fi->attr_version > attr_version) {
  147. spin_unlock(&fc->lock);
  148. return;
  149. }
  150. fuse_change_attributes_common(inode, attr, attr_valid);
  151. oldsize = inode->i_size;
  152. i_size_write(inode, attr->size);
  153. spin_unlock(&fc->lock);
  154. if (S_ISREG(inode->i_mode) && oldsize != attr->size) {
  155. if (attr->size < oldsize)
  156. fuse_truncate(inode->i_mapping, attr->size);
  157. invalidate_inode_pages2(inode->i_mapping);
  158. }
  159. }
  160. static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
  161. {
  162. inode->i_mode = attr->mode & S_IFMT;
  163. inode->i_size = attr->size;
  164. if (S_ISREG(inode->i_mode)) {
  165. fuse_init_common(inode);
  166. fuse_init_file_inode(inode);
  167. } else if (S_ISDIR(inode->i_mode))
  168. fuse_init_dir(inode);
  169. else if (S_ISLNK(inode->i_mode))
  170. fuse_init_symlink(inode);
  171. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  172. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  173. fuse_init_common(inode);
  174. init_special_inode(inode, inode->i_mode,
  175. new_decode_dev(attr->rdev));
  176. } else
  177. BUG();
  178. }
  179. int fuse_inode_eq(struct inode *inode, void *_nodeidp)
  180. {
  181. u64 nodeid = *(u64 *) _nodeidp;
  182. if (get_node_id(inode) == nodeid)
  183. return 1;
  184. else
  185. return 0;
  186. }
  187. static int fuse_inode_set(struct inode *inode, void *_nodeidp)
  188. {
  189. u64 nodeid = *(u64 *) _nodeidp;
  190. get_fuse_inode(inode)->nodeid = nodeid;
  191. return 0;
  192. }
  193. struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
  194. int generation, struct fuse_attr *attr,
  195. u64 attr_valid, u64 attr_version)
  196. {
  197. struct inode *inode;
  198. struct fuse_inode *fi;
  199. struct fuse_conn *fc = get_fuse_conn_super(sb);
  200. retry:
  201. inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
  202. if (!inode)
  203. return NULL;
  204. if ((inode->i_state & I_NEW)) {
  205. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  206. inode->i_generation = generation;
  207. inode->i_data.backing_dev_info = &fc->bdi;
  208. fuse_init_inode(inode, attr);
  209. unlock_new_inode(inode);
  210. } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
  211. /* Inode has changed type, any I/O on the old should fail */
  212. make_bad_inode(inode);
  213. iput(inode);
  214. goto retry;
  215. }
  216. fi = get_fuse_inode(inode);
  217. spin_lock(&fc->lock);
  218. fi->nlookup++;
  219. spin_unlock(&fc->lock);
  220. fuse_change_attributes(inode, attr, attr_valid, attr_version);
  221. return inode;
  222. }
  223. int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
  224. loff_t offset, loff_t len)
  225. {
  226. struct inode *inode;
  227. pgoff_t pg_start;
  228. pgoff_t pg_end;
  229. inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
  230. if (!inode)
  231. return -ENOENT;
  232. fuse_invalidate_attr(inode);
  233. if (offset >= 0) {
  234. pg_start = offset >> PAGE_CACHE_SHIFT;
  235. if (len <= 0)
  236. pg_end = -1;
  237. else
  238. pg_end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  239. invalidate_inode_pages2_range(inode->i_mapping,
  240. pg_start, pg_end);
  241. }
  242. iput(inode);
  243. return 0;
  244. }
  245. static void fuse_umount_begin(struct super_block *sb)
  246. {
  247. fuse_abort_conn(get_fuse_conn_super(sb));
  248. }
  249. static void fuse_send_destroy(struct fuse_conn *fc)
  250. {
  251. struct fuse_req *req = fc->destroy_req;
  252. if (req && fc->conn_init) {
  253. fc->destroy_req = NULL;
  254. req->in.h.opcode = FUSE_DESTROY;
  255. req->force = 1;
  256. fuse_request_send(fc, req);
  257. fuse_put_request(fc, req);
  258. }
  259. }
  260. static void fuse_bdi_destroy(struct fuse_conn *fc)
  261. {
  262. if (fc->bdi_initialized)
  263. bdi_destroy(&fc->bdi);
  264. }
  265. void fuse_conn_kill(struct fuse_conn *fc)
  266. {
  267. spin_lock(&fc->lock);
  268. fc->connected = 0;
  269. fc->blocked = 0;
  270. spin_unlock(&fc->lock);
  271. /* Flush all readers on this fs */
  272. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  273. wake_up_all(&fc->waitq);
  274. wake_up_all(&fc->blocked_waitq);
  275. wake_up_all(&fc->reserved_req_waitq);
  276. mutex_lock(&fuse_mutex);
  277. list_del(&fc->entry);
  278. fuse_ctl_remove_conn(fc);
  279. mutex_unlock(&fuse_mutex);
  280. fuse_bdi_destroy(fc);
  281. }
  282. EXPORT_SYMBOL_GPL(fuse_conn_kill);
  283. static void fuse_put_super(struct super_block *sb)
  284. {
  285. struct fuse_conn *fc = get_fuse_conn_super(sb);
  286. fuse_send_destroy(fc);
  287. fuse_conn_kill(fc);
  288. fuse_conn_put(fc);
  289. }
  290. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  291. {
  292. stbuf->f_type = FUSE_SUPER_MAGIC;
  293. stbuf->f_bsize = attr->bsize;
  294. stbuf->f_frsize = attr->frsize;
  295. stbuf->f_blocks = attr->blocks;
  296. stbuf->f_bfree = attr->bfree;
  297. stbuf->f_bavail = attr->bavail;
  298. stbuf->f_files = attr->files;
  299. stbuf->f_ffree = attr->ffree;
  300. stbuf->f_namelen = attr->namelen;
  301. /* fsid is left zero */
  302. }
  303. static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
  304. {
  305. struct super_block *sb = dentry->d_sb;
  306. struct fuse_conn *fc = get_fuse_conn_super(sb);
  307. struct fuse_req *req;
  308. struct fuse_statfs_out outarg;
  309. int err;
  310. if (!fuse_allow_task(fc, current)) {
  311. buf->f_type = FUSE_SUPER_MAGIC;
  312. return 0;
  313. }
  314. req = fuse_get_req(fc);
  315. if (IS_ERR(req))
  316. return PTR_ERR(req);
  317. memset(&outarg, 0, sizeof(outarg));
  318. req->in.numargs = 0;
  319. req->in.h.opcode = FUSE_STATFS;
  320. req->in.h.nodeid = get_node_id(dentry->d_inode);
  321. req->out.numargs = 1;
  322. req->out.args[0].size =
  323. fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
  324. req->out.args[0].value = &outarg;
  325. fuse_request_send(fc, req);
  326. err = req->out.h.error;
  327. if (!err)
  328. convert_fuse_statfs(buf, &outarg.st);
  329. fuse_put_request(fc, req);
  330. return err;
  331. }
  332. enum {
  333. OPT_FD,
  334. OPT_ROOTMODE,
  335. OPT_USER_ID,
  336. OPT_GROUP_ID,
  337. OPT_DEFAULT_PERMISSIONS,
  338. OPT_ALLOW_OTHER,
  339. OPT_MAX_READ,
  340. OPT_BLKSIZE,
  341. OPT_ERR
  342. };
  343. static const match_table_t tokens = {
  344. {OPT_FD, "fd=%u"},
  345. {OPT_ROOTMODE, "rootmode=%o"},
  346. {OPT_USER_ID, "user_id=%u"},
  347. {OPT_GROUP_ID, "group_id=%u"},
  348. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  349. {OPT_ALLOW_OTHER, "allow_other"},
  350. {OPT_MAX_READ, "max_read=%u"},
  351. {OPT_BLKSIZE, "blksize=%u"},
  352. {OPT_ERR, NULL}
  353. };
  354. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
  355. {
  356. char *p;
  357. memset(d, 0, sizeof(struct fuse_mount_data));
  358. d->max_read = ~0;
  359. d->blksize = FUSE_DEFAULT_BLKSIZE;
  360. while ((p = strsep(&opt, ",")) != NULL) {
  361. int token;
  362. int value;
  363. substring_t args[MAX_OPT_ARGS];
  364. if (!*p)
  365. continue;
  366. token = match_token(p, tokens, args);
  367. switch (token) {
  368. case OPT_FD:
  369. if (match_int(&args[0], &value))
  370. return 0;
  371. d->fd = value;
  372. d->fd_present = 1;
  373. break;
  374. case OPT_ROOTMODE:
  375. if (match_octal(&args[0], &value))
  376. return 0;
  377. if (!fuse_valid_type(value))
  378. return 0;
  379. d->rootmode = value;
  380. d->rootmode_present = 1;
  381. break;
  382. case OPT_USER_ID:
  383. if (match_int(&args[0], &value))
  384. return 0;
  385. d->user_id = value;
  386. d->user_id_present = 1;
  387. break;
  388. case OPT_GROUP_ID:
  389. if (match_int(&args[0], &value))
  390. return 0;
  391. d->group_id = value;
  392. d->group_id_present = 1;
  393. break;
  394. case OPT_DEFAULT_PERMISSIONS:
  395. d->flags |= FUSE_DEFAULT_PERMISSIONS;
  396. break;
  397. case OPT_ALLOW_OTHER:
  398. d->flags |= FUSE_ALLOW_OTHER;
  399. break;
  400. case OPT_MAX_READ:
  401. if (match_int(&args[0], &value))
  402. return 0;
  403. d->max_read = value;
  404. break;
  405. case OPT_BLKSIZE:
  406. if (!is_bdev || match_int(&args[0], &value))
  407. return 0;
  408. d->blksize = value;
  409. break;
  410. default:
  411. return 0;
  412. }
  413. }
  414. if (!d->fd_present || !d->rootmode_present ||
  415. !d->user_id_present || !d->group_id_present)
  416. return 0;
  417. return 1;
  418. }
  419. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  420. {
  421. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  422. seq_printf(m, ",user_id=%u", fc->user_id);
  423. seq_printf(m, ",group_id=%u", fc->group_id);
  424. if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
  425. seq_puts(m, ",default_permissions");
  426. if (fc->flags & FUSE_ALLOW_OTHER)
  427. seq_puts(m, ",allow_other");
  428. if (fc->max_read != ~0)
  429. seq_printf(m, ",max_read=%u", fc->max_read);
  430. if (mnt->mnt_sb->s_bdev &&
  431. mnt->mnt_sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
  432. seq_printf(m, ",blksize=%lu", mnt->mnt_sb->s_blocksize);
  433. return 0;
  434. }
  435. void fuse_conn_init(struct fuse_conn *fc)
  436. {
  437. memset(fc, 0, sizeof(*fc));
  438. spin_lock_init(&fc->lock);
  439. mutex_init(&fc->inst_mutex);
  440. init_rwsem(&fc->killsb);
  441. atomic_set(&fc->count, 1);
  442. init_waitqueue_head(&fc->waitq);
  443. init_waitqueue_head(&fc->blocked_waitq);
  444. init_waitqueue_head(&fc->reserved_req_waitq);
  445. INIT_LIST_HEAD(&fc->pending);
  446. INIT_LIST_HEAD(&fc->processing);
  447. INIT_LIST_HEAD(&fc->io);
  448. INIT_LIST_HEAD(&fc->interrupts);
  449. INIT_LIST_HEAD(&fc->bg_queue);
  450. INIT_LIST_HEAD(&fc->entry);
  451. atomic_set(&fc->num_waiting, 0);
  452. fc->khctr = 0;
  453. fc->polled_files = RB_ROOT;
  454. fc->reqctr = 0;
  455. fc->blocked = 1;
  456. fc->attr_version = 1;
  457. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  458. }
  459. EXPORT_SYMBOL_GPL(fuse_conn_init);
  460. void fuse_conn_put(struct fuse_conn *fc)
  461. {
  462. if (atomic_dec_and_test(&fc->count)) {
  463. if (fc->destroy_req)
  464. fuse_request_free(fc->destroy_req);
  465. mutex_destroy(&fc->inst_mutex);
  466. fc->release(fc);
  467. }
  468. }
  469. EXPORT_SYMBOL_GPL(fuse_conn_put);
  470. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  471. {
  472. atomic_inc(&fc->count);
  473. return fc;
  474. }
  475. EXPORT_SYMBOL_GPL(fuse_conn_get);
  476. static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
  477. {
  478. struct fuse_attr attr;
  479. memset(&attr, 0, sizeof(attr));
  480. attr.mode = mode;
  481. attr.ino = FUSE_ROOT_ID;
  482. attr.nlink = 1;
  483. return fuse_iget(sb, 1, 0, &attr, 0, 0);
  484. }
  485. struct fuse_inode_handle {
  486. u64 nodeid;
  487. u32 generation;
  488. };
  489. static struct dentry *fuse_get_dentry(struct super_block *sb,
  490. struct fuse_inode_handle *handle)
  491. {
  492. struct fuse_conn *fc = get_fuse_conn_super(sb);
  493. struct inode *inode;
  494. struct dentry *entry;
  495. int err = -ESTALE;
  496. if (handle->nodeid == 0)
  497. goto out_err;
  498. inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
  499. if (!inode) {
  500. struct fuse_entry_out outarg;
  501. struct qstr name;
  502. if (!fc->export_support)
  503. goto out_err;
  504. name.len = 1;
  505. name.name = ".";
  506. err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
  507. &inode);
  508. if (err && err != -ENOENT)
  509. goto out_err;
  510. if (err || !inode) {
  511. err = -ESTALE;
  512. goto out_err;
  513. }
  514. err = -EIO;
  515. if (get_node_id(inode) != handle->nodeid)
  516. goto out_iput;
  517. }
  518. err = -ESTALE;
  519. if (inode->i_generation != handle->generation)
  520. goto out_iput;
  521. entry = d_obtain_alias(inode);
  522. if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) {
  523. entry->d_op = &fuse_dentry_operations;
  524. fuse_invalidate_entry_cache(entry);
  525. }
  526. return entry;
  527. out_iput:
  528. iput(inode);
  529. out_err:
  530. return ERR_PTR(err);
  531. }
  532. static int fuse_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
  533. int connectable)
  534. {
  535. struct inode *inode = dentry->d_inode;
  536. bool encode_parent = connectable && !S_ISDIR(inode->i_mode);
  537. int len = encode_parent ? 6 : 3;
  538. u64 nodeid;
  539. u32 generation;
  540. if (*max_len < len)
  541. return 255;
  542. nodeid = get_fuse_inode(inode)->nodeid;
  543. generation = inode->i_generation;
  544. fh[0] = (u32)(nodeid >> 32);
  545. fh[1] = (u32)(nodeid & 0xffffffff);
  546. fh[2] = generation;
  547. if (encode_parent) {
  548. struct inode *parent;
  549. spin_lock(&dentry->d_lock);
  550. parent = dentry->d_parent->d_inode;
  551. nodeid = get_fuse_inode(parent)->nodeid;
  552. generation = parent->i_generation;
  553. spin_unlock(&dentry->d_lock);
  554. fh[3] = (u32)(nodeid >> 32);
  555. fh[4] = (u32)(nodeid & 0xffffffff);
  556. fh[5] = generation;
  557. }
  558. *max_len = len;
  559. return encode_parent ? 0x82 : 0x81;
  560. }
  561. static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
  562. struct fid *fid, int fh_len, int fh_type)
  563. {
  564. struct fuse_inode_handle handle;
  565. if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
  566. return NULL;
  567. handle.nodeid = (u64) fid->raw[0] << 32;
  568. handle.nodeid |= (u64) fid->raw[1];
  569. handle.generation = fid->raw[2];
  570. return fuse_get_dentry(sb, &handle);
  571. }
  572. static struct dentry *fuse_fh_to_parent(struct super_block *sb,
  573. struct fid *fid, int fh_len, int fh_type)
  574. {
  575. struct fuse_inode_handle parent;
  576. if (fh_type != 0x82 || fh_len < 6)
  577. return NULL;
  578. parent.nodeid = (u64) fid->raw[3] << 32;
  579. parent.nodeid |= (u64) fid->raw[4];
  580. parent.generation = fid->raw[5];
  581. return fuse_get_dentry(sb, &parent);
  582. }
  583. static struct dentry *fuse_get_parent(struct dentry *child)
  584. {
  585. struct inode *child_inode = child->d_inode;
  586. struct fuse_conn *fc = get_fuse_conn(child_inode);
  587. struct inode *inode;
  588. struct dentry *parent;
  589. struct fuse_entry_out outarg;
  590. struct qstr name;
  591. int err;
  592. if (!fc->export_support)
  593. return ERR_PTR(-ESTALE);
  594. name.len = 2;
  595. name.name = "..";
  596. err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
  597. &name, &outarg, &inode);
  598. if (err) {
  599. if (err == -ENOENT)
  600. return ERR_PTR(-ESTALE);
  601. return ERR_PTR(err);
  602. }
  603. parent = d_obtain_alias(inode);
  604. if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) {
  605. parent->d_op = &fuse_dentry_operations;
  606. fuse_invalidate_entry_cache(parent);
  607. }
  608. return parent;
  609. }
  610. static const struct export_operations fuse_export_operations = {
  611. .fh_to_dentry = fuse_fh_to_dentry,
  612. .fh_to_parent = fuse_fh_to_parent,
  613. .encode_fh = fuse_encode_fh,
  614. .get_parent = fuse_get_parent,
  615. };
  616. static const struct super_operations fuse_super_operations = {
  617. .alloc_inode = fuse_alloc_inode,
  618. .destroy_inode = fuse_destroy_inode,
  619. .clear_inode = fuse_clear_inode,
  620. .drop_inode = generic_delete_inode,
  621. .remount_fs = fuse_remount_fs,
  622. .put_super = fuse_put_super,
  623. .umount_begin = fuse_umount_begin,
  624. .statfs = fuse_statfs,
  625. .show_options = fuse_show_options,
  626. };
  627. static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
  628. {
  629. struct fuse_init_out *arg = &req->misc.init_out;
  630. if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
  631. fc->conn_error = 1;
  632. else {
  633. unsigned long ra_pages;
  634. if (arg->minor >= 6) {
  635. ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
  636. if (arg->flags & FUSE_ASYNC_READ)
  637. fc->async_read = 1;
  638. if (!(arg->flags & FUSE_POSIX_LOCKS))
  639. fc->no_lock = 1;
  640. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  641. fc->atomic_o_trunc = 1;
  642. if (arg->minor >= 9) {
  643. /* LOOKUP has dependency on proto version */
  644. if (arg->flags & FUSE_EXPORT_SUPPORT)
  645. fc->export_support = 1;
  646. }
  647. if (arg->flags & FUSE_BIG_WRITES)
  648. fc->big_writes = 1;
  649. if (arg->flags & FUSE_DONT_MASK)
  650. fc->dont_mask = 1;
  651. } else {
  652. ra_pages = fc->max_read / PAGE_CACHE_SIZE;
  653. fc->no_lock = 1;
  654. }
  655. fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
  656. fc->minor = arg->minor;
  657. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  658. fc->max_write = max_t(unsigned, 4096, fc->max_write);
  659. fc->conn_init = 1;
  660. }
  661. fc->blocked = 0;
  662. wake_up_all(&fc->blocked_waitq);
  663. }
  664. static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
  665. {
  666. struct fuse_init_in *arg = &req->misc.init_in;
  667. arg->major = FUSE_KERNEL_VERSION;
  668. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  669. arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
  670. arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
  671. FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK;
  672. req->in.h.opcode = FUSE_INIT;
  673. req->in.numargs = 1;
  674. req->in.args[0].size = sizeof(*arg);
  675. req->in.args[0].value = arg;
  676. req->out.numargs = 1;
  677. /* Variable length arguement used for backward compatibility
  678. with interface version < 7.5. Rest of init_out is zeroed
  679. by do_get_request(), so a short reply is not a problem */
  680. req->out.argvar = 1;
  681. req->out.args[0].size = sizeof(struct fuse_init_out);
  682. req->out.args[0].value = &req->misc.init_out;
  683. req->end = process_init_reply;
  684. fuse_request_send_background(fc, req);
  685. }
  686. static void fuse_free_conn(struct fuse_conn *fc)
  687. {
  688. kfree(fc);
  689. }
  690. static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
  691. {
  692. int err;
  693. fc->bdi.name = "fuse";
  694. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  695. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  696. /* fuse does it's own writeback accounting */
  697. fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB;
  698. err = bdi_init(&fc->bdi);
  699. if (err)
  700. return err;
  701. fc->bdi_initialized = 1;
  702. if (sb->s_bdev) {
  703. err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
  704. MAJOR(fc->dev), MINOR(fc->dev));
  705. } else {
  706. err = bdi_register_dev(&fc->bdi, fc->dev);
  707. }
  708. if (err)
  709. return err;
  710. /*
  711. * For a single fuse filesystem use max 1% of dirty +
  712. * writeback threshold.
  713. *
  714. * This gives about 1M of write buffer for memory maps on a
  715. * machine with 1G and 10% dirty_ratio, which should be more
  716. * than enough.
  717. *
  718. * Privileged users can raise it by writing to
  719. *
  720. * /sys/class/bdi/<bdi>/max_ratio
  721. */
  722. bdi_set_max_ratio(&fc->bdi, 1);
  723. return 0;
  724. }
  725. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  726. {
  727. struct fuse_conn *fc;
  728. struct inode *root;
  729. struct fuse_mount_data d;
  730. struct file *file;
  731. struct dentry *root_dentry;
  732. struct fuse_req *init_req;
  733. int err;
  734. int is_bdev = sb->s_bdev != NULL;
  735. err = -EINVAL;
  736. if (sb->s_flags & MS_MANDLOCK)
  737. goto err;
  738. if (!parse_fuse_opt((char *) data, &d, is_bdev))
  739. goto err;
  740. if (is_bdev) {
  741. #ifdef CONFIG_BLOCK
  742. err = -EINVAL;
  743. if (!sb_set_blocksize(sb, d.blksize))
  744. goto err;
  745. #endif
  746. } else {
  747. sb->s_blocksize = PAGE_CACHE_SIZE;
  748. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  749. }
  750. sb->s_magic = FUSE_SUPER_MAGIC;
  751. sb->s_op = &fuse_super_operations;
  752. sb->s_maxbytes = MAX_LFS_FILESIZE;
  753. sb->s_export_op = &fuse_export_operations;
  754. file = fget(d.fd);
  755. err = -EINVAL;
  756. if (!file)
  757. goto err;
  758. if (file->f_op != &fuse_dev_operations)
  759. goto err_fput;
  760. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  761. err = -ENOMEM;
  762. if (!fc)
  763. goto err_fput;
  764. fuse_conn_init(fc);
  765. fc->dev = sb->s_dev;
  766. fc->sb = sb;
  767. err = fuse_bdi_init(fc, sb);
  768. if (err)
  769. goto err_put_conn;
  770. /* Handle umasking inside the fuse code */
  771. if (sb->s_flags & MS_POSIXACL)
  772. fc->dont_mask = 1;
  773. sb->s_flags |= MS_POSIXACL;
  774. fc->release = fuse_free_conn;
  775. fc->flags = d.flags;
  776. fc->user_id = d.user_id;
  777. fc->group_id = d.group_id;
  778. fc->max_read = max_t(unsigned, 4096, d.max_read);
  779. /* Used by get_root_inode() */
  780. sb->s_fs_info = fc;
  781. err = -ENOMEM;
  782. root = fuse_get_root_inode(sb, d.rootmode);
  783. if (!root)
  784. goto err_put_conn;
  785. root_dentry = d_alloc_root(root);
  786. if (!root_dentry) {
  787. iput(root);
  788. goto err_put_conn;
  789. }
  790. init_req = fuse_request_alloc();
  791. if (!init_req)
  792. goto err_put_root;
  793. if (is_bdev) {
  794. fc->destroy_req = fuse_request_alloc();
  795. if (!fc->destroy_req)
  796. goto err_free_init_req;
  797. }
  798. mutex_lock(&fuse_mutex);
  799. err = -EINVAL;
  800. if (file->private_data)
  801. goto err_unlock;
  802. err = fuse_ctl_add_conn(fc);
  803. if (err)
  804. goto err_unlock;
  805. list_add_tail(&fc->entry, &fuse_conn_list);
  806. sb->s_root = root_dentry;
  807. fc->connected = 1;
  808. file->private_data = fuse_conn_get(fc);
  809. mutex_unlock(&fuse_mutex);
  810. /*
  811. * atomic_dec_and_test() in fput() provides the necessary
  812. * memory barrier for file->private_data to be visible on all
  813. * CPUs after this
  814. */
  815. fput(file);
  816. fuse_send_init(fc, init_req);
  817. return 0;
  818. err_unlock:
  819. mutex_unlock(&fuse_mutex);
  820. err_free_init_req:
  821. fuse_request_free(init_req);
  822. err_put_root:
  823. dput(root_dentry);
  824. err_put_conn:
  825. fuse_bdi_destroy(fc);
  826. fuse_conn_put(fc);
  827. err_fput:
  828. fput(file);
  829. err:
  830. return err;
  831. }
  832. static int fuse_get_sb(struct file_system_type *fs_type,
  833. int flags, const char *dev_name,
  834. void *raw_data, struct vfsmount *mnt)
  835. {
  836. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
  837. }
  838. static void fuse_kill_sb_anon(struct super_block *sb)
  839. {
  840. struct fuse_conn *fc = get_fuse_conn_super(sb);
  841. if (fc) {
  842. down_write(&fc->killsb);
  843. fc->sb = NULL;
  844. up_write(&fc->killsb);
  845. }
  846. kill_anon_super(sb);
  847. }
  848. static struct file_system_type fuse_fs_type = {
  849. .owner = THIS_MODULE,
  850. .name = "fuse",
  851. .fs_flags = FS_HAS_SUBTYPE,
  852. .get_sb = fuse_get_sb,
  853. .kill_sb = fuse_kill_sb_anon,
  854. };
  855. #ifdef CONFIG_BLOCK
  856. static int fuse_get_sb_blk(struct file_system_type *fs_type,
  857. int flags, const char *dev_name,
  858. void *raw_data, struct vfsmount *mnt)
  859. {
  860. return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super,
  861. mnt);
  862. }
  863. static void fuse_kill_sb_blk(struct super_block *sb)
  864. {
  865. struct fuse_conn *fc = get_fuse_conn_super(sb);
  866. if (fc) {
  867. down_write(&fc->killsb);
  868. fc->sb = NULL;
  869. up_write(&fc->killsb);
  870. }
  871. kill_block_super(sb);
  872. }
  873. static struct file_system_type fuseblk_fs_type = {
  874. .owner = THIS_MODULE,
  875. .name = "fuseblk",
  876. .get_sb = fuse_get_sb_blk,
  877. .kill_sb = fuse_kill_sb_blk,
  878. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  879. };
  880. static inline int register_fuseblk(void)
  881. {
  882. return register_filesystem(&fuseblk_fs_type);
  883. }
  884. static inline void unregister_fuseblk(void)
  885. {
  886. unregister_filesystem(&fuseblk_fs_type);
  887. }
  888. #else
  889. static inline int register_fuseblk(void)
  890. {
  891. return 0;
  892. }
  893. static inline void unregister_fuseblk(void)
  894. {
  895. }
  896. #endif
  897. static void fuse_inode_init_once(void *foo)
  898. {
  899. struct inode *inode = foo;
  900. inode_init_once(inode);
  901. }
  902. static int __init fuse_fs_init(void)
  903. {
  904. int err;
  905. err = register_filesystem(&fuse_fs_type);
  906. if (err)
  907. goto out;
  908. err = register_fuseblk();
  909. if (err)
  910. goto out_unreg;
  911. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  912. sizeof(struct fuse_inode),
  913. 0, SLAB_HWCACHE_ALIGN,
  914. fuse_inode_init_once);
  915. err = -ENOMEM;
  916. if (!fuse_inode_cachep)
  917. goto out_unreg2;
  918. return 0;
  919. out_unreg2:
  920. unregister_fuseblk();
  921. out_unreg:
  922. unregister_filesystem(&fuse_fs_type);
  923. out:
  924. return err;
  925. }
  926. static void fuse_fs_cleanup(void)
  927. {
  928. unregister_filesystem(&fuse_fs_type);
  929. unregister_fuseblk();
  930. kmem_cache_destroy(fuse_inode_cachep);
  931. }
  932. static struct kobject *fuse_kobj;
  933. static struct kobject *connections_kobj;
  934. static int fuse_sysfs_init(void)
  935. {
  936. int err;
  937. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  938. if (!fuse_kobj) {
  939. err = -ENOMEM;
  940. goto out_err;
  941. }
  942. connections_kobj = kobject_create_and_add("connections", fuse_kobj);
  943. if (!connections_kobj) {
  944. err = -ENOMEM;
  945. goto out_fuse_unregister;
  946. }
  947. return 0;
  948. out_fuse_unregister:
  949. kobject_put(fuse_kobj);
  950. out_err:
  951. return err;
  952. }
  953. static void fuse_sysfs_cleanup(void)
  954. {
  955. kobject_put(connections_kobj);
  956. kobject_put(fuse_kobj);
  957. }
  958. static int __init fuse_init(void)
  959. {
  960. int res;
  961. printk(KERN_INFO "fuse init (API version %i.%i)\n",
  962. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  963. INIT_LIST_HEAD(&fuse_conn_list);
  964. res = fuse_fs_init();
  965. if (res)
  966. goto err;
  967. res = fuse_dev_init();
  968. if (res)
  969. goto err_fs_cleanup;
  970. res = fuse_sysfs_init();
  971. if (res)
  972. goto err_dev_cleanup;
  973. res = fuse_ctl_init();
  974. if (res)
  975. goto err_sysfs_cleanup;
  976. return 0;
  977. err_sysfs_cleanup:
  978. fuse_sysfs_cleanup();
  979. err_dev_cleanup:
  980. fuse_dev_cleanup();
  981. err_fs_cleanup:
  982. fuse_fs_cleanup();
  983. err:
  984. return res;
  985. }
  986. static void __exit fuse_exit(void)
  987. {
  988. printk(KERN_DEBUG "fuse exit\n");
  989. fuse_ctl_cleanup();
  990. fuse_sysfs_cleanup();
  991. fuse_fs_cleanup();
  992. fuse_dev_cleanup();
  993. }
  994. module_init(fuse_init);
  995. module_exit(fuse_exit);