inode.c 25 KB

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