inode.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. static 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. static void fuse_umount_begin(struct super_block *sb)
  224. {
  225. fuse_abort_conn(get_fuse_conn_super(sb));
  226. }
  227. static void fuse_send_destroy(struct fuse_conn *fc)
  228. {
  229. struct fuse_req *req = fc->destroy_req;
  230. if (req && fc->conn_init) {
  231. fc->destroy_req = NULL;
  232. req->in.h.opcode = FUSE_DESTROY;
  233. req->force = 1;
  234. fuse_request_send(fc, req);
  235. fuse_put_request(fc, req);
  236. }
  237. }
  238. static void fuse_put_super(struct super_block *sb)
  239. {
  240. struct fuse_conn *fc = get_fuse_conn_super(sb);
  241. fuse_send_destroy(fc);
  242. spin_lock(&fc->lock);
  243. fc->connected = 0;
  244. fc->blocked = 0;
  245. spin_unlock(&fc->lock);
  246. /* Flush all readers on this fs */
  247. kill_fasync(&fc->fasync, SIGIO, POLL_IN);
  248. wake_up_all(&fc->waitq);
  249. wake_up_all(&fc->blocked_waitq);
  250. wake_up_all(&fc->reserved_req_waitq);
  251. mutex_lock(&fuse_mutex);
  252. list_del(&fc->entry);
  253. fuse_ctl_remove_conn(fc);
  254. mutex_unlock(&fuse_mutex);
  255. fuse_conn_put(fc);
  256. }
  257. static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
  258. {
  259. stbuf->f_type = FUSE_SUPER_MAGIC;
  260. stbuf->f_bsize = attr->bsize;
  261. stbuf->f_frsize = attr->frsize;
  262. stbuf->f_blocks = attr->blocks;
  263. stbuf->f_bfree = attr->bfree;
  264. stbuf->f_bavail = attr->bavail;
  265. stbuf->f_files = attr->files;
  266. stbuf->f_ffree = attr->ffree;
  267. stbuf->f_namelen = attr->namelen;
  268. /* fsid is left zero */
  269. }
  270. static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
  271. {
  272. struct super_block *sb = dentry->d_sb;
  273. struct fuse_conn *fc = get_fuse_conn_super(sb);
  274. struct fuse_req *req;
  275. struct fuse_statfs_out outarg;
  276. int err;
  277. if (!fuse_allow_task(fc, current)) {
  278. buf->f_type = FUSE_SUPER_MAGIC;
  279. return 0;
  280. }
  281. req = fuse_get_req(fc);
  282. if (IS_ERR(req))
  283. return PTR_ERR(req);
  284. memset(&outarg, 0, sizeof(outarg));
  285. req->in.numargs = 0;
  286. req->in.h.opcode = FUSE_STATFS;
  287. req->in.h.nodeid = get_node_id(dentry->d_inode);
  288. req->out.numargs = 1;
  289. req->out.args[0].size =
  290. fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
  291. req->out.args[0].value = &outarg;
  292. fuse_request_send(fc, req);
  293. err = req->out.h.error;
  294. if (!err)
  295. convert_fuse_statfs(buf, &outarg.st);
  296. fuse_put_request(fc, req);
  297. return err;
  298. }
  299. enum {
  300. OPT_FD,
  301. OPT_ROOTMODE,
  302. OPT_USER_ID,
  303. OPT_GROUP_ID,
  304. OPT_DEFAULT_PERMISSIONS,
  305. OPT_ALLOW_OTHER,
  306. OPT_MAX_READ,
  307. OPT_BLKSIZE,
  308. OPT_ERR
  309. };
  310. static const match_table_t tokens = {
  311. {OPT_FD, "fd=%u"},
  312. {OPT_ROOTMODE, "rootmode=%o"},
  313. {OPT_USER_ID, "user_id=%u"},
  314. {OPT_GROUP_ID, "group_id=%u"},
  315. {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
  316. {OPT_ALLOW_OTHER, "allow_other"},
  317. {OPT_MAX_READ, "max_read=%u"},
  318. {OPT_BLKSIZE, "blksize=%u"},
  319. {OPT_ERR, NULL}
  320. };
  321. static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
  322. {
  323. char *p;
  324. memset(d, 0, sizeof(struct fuse_mount_data));
  325. d->max_read = ~0;
  326. d->blksize = FUSE_DEFAULT_BLKSIZE;
  327. while ((p = strsep(&opt, ",")) != NULL) {
  328. int token;
  329. int value;
  330. substring_t args[MAX_OPT_ARGS];
  331. if (!*p)
  332. continue;
  333. token = match_token(p, tokens, args);
  334. switch (token) {
  335. case OPT_FD:
  336. if (match_int(&args[0], &value))
  337. return 0;
  338. d->fd = value;
  339. d->fd_present = 1;
  340. break;
  341. case OPT_ROOTMODE:
  342. if (match_octal(&args[0], &value))
  343. return 0;
  344. if (!fuse_valid_type(value))
  345. return 0;
  346. d->rootmode = value;
  347. d->rootmode_present = 1;
  348. break;
  349. case OPT_USER_ID:
  350. if (match_int(&args[0], &value))
  351. return 0;
  352. d->user_id = value;
  353. d->user_id_present = 1;
  354. break;
  355. case OPT_GROUP_ID:
  356. if (match_int(&args[0], &value))
  357. return 0;
  358. d->group_id = value;
  359. d->group_id_present = 1;
  360. break;
  361. case OPT_DEFAULT_PERMISSIONS:
  362. d->flags |= FUSE_DEFAULT_PERMISSIONS;
  363. break;
  364. case OPT_ALLOW_OTHER:
  365. d->flags |= FUSE_ALLOW_OTHER;
  366. break;
  367. case OPT_MAX_READ:
  368. if (match_int(&args[0], &value))
  369. return 0;
  370. d->max_read = value;
  371. break;
  372. case OPT_BLKSIZE:
  373. if (!is_bdev || match_int(&args[0], &value))
  374. return 0;
  375. d->blksize = value;
  376. break;
  377. default:
  378. return 0;
  379. }
  380. }
  381. if (!d->fd_present || !d->rootmode_present ||
  382. !d->user_id_present || !d->group_id_present)
  383. return 0;
  384. return 1;
  385. }
  386. static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
  387. {
  388. struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
  389. seq_printf(m, ",user_id=%u", fc->user_id);
  390. seq_printf(m, ",group_id=%u", fc->group_id);
  391. if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
  392. seq_puts(m, ",default_permissions");
  393. if (fc->flags & FUSE_ALLOW_OTHER)
  394. seq_puts(m, ",allow_other");
  395. if (fc->max_read != ~0)
  396. seq_printf(m, ",max_read=%u", fc->max_read);
  397. if (mnt->mnt_sb->s_bdev &&
  398. mnt->mnt_sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
  399. seq_printf(m, ",blksize=%lu", mnt->mnt_sb->s_blocksize);
  400. return 0;
  401. }
  402. int fuse_conn_init(struct fuse_conn *fc, struct super_block *sb)
  403. {
  404. int err;
  405. memset(fc, 0, sizeof(*fc));
  406. spin_lock_init(&fc->lock);
  407. mutex_init(&fc->inst_mutex);
  408. atomic_set(&fc->count, 1);
  409. init_waitqueue_head(&fc->waitq);
  410. init_waitqueue_head(&fc->blocked_waitq);
  411. init_waitqueue_head(&fc->reserved_req_waitq);
  412. INIT_LIST_HEAD(&fc->pending);
  413. INIT_LIST_HEAD(&fc->processing);
  414. INIT_LIST_HEAD(&fc->io);
  415. INIT_LIST_HEAD(&fc->interrupts);
  416. INIT_LIST_HEAD(&fc->bg_queue);
  417. INIT_LIST_HEAD(&fc->entry);
  418. atomic_set(&fc->num_waiting, 0);
  419. fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  420. fc->bdi.unplug_io_fn = default_unplug_io_fn;
  421. /* fuse does it's own writeback accounting */
  422. fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB;
  423. fc->khctr = 0;
  424. fc->polled_files = RB_ROOT;
  425. fc->dev = sb->s_dev;
  426. err = bdi_init(&fc->bdi);
  427. if (err)
  428. goto error_mutex_destroy;
  429. if (sb->s_bdev) {
  430. err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
  431. MAJOR(fc->dev), MINOR(fc->dev));
  432. } else {
  433. err = bdi_register_dev(&fc->bdi, fc->dev);
  434. }
  435. if (err)
  436. goto error_bdi_destroy;
  437. /*
  438. * For a single fuse filesystem use max 1% of dirty +
  439. * writeback threshold.
  440. *
  441. * This gives about 1M of write buffer for memory maps on a
  442. * machine with 1G and 10% dirty_ratio, which should be more
  443. * than enough.
  444. *
  445. * Privileged users can raise it by writing to
  446. *
  447. * /sys/class/bdi/<bdi>/max_ratio
  448. */
  449. bdi_set_max_ratio(&fc->bdi, 1);
  450. fc->reqctr = 0;
  451. fc->blocked = 1;
  452. fc->attr_version = 1;
  453. get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
  454. return 0;
  455. error_bdi_destroy:
  456. bdi_destroy(&fc->bdi);
  457. error_mutex_destroy:
  458. mutex_destroy(&fc->inst_mutex);
  459. return err;
  460. }
  461. EXPORT_SYMBOL_GPL(fuse_conn_init);
  462. void fuse_conn_put(struct fuse_conn *fc)
  463. {
  464. if (atomic_dec_and_test(&fc->count)) {
  465. if (fc->destroy_req)
  466. fuse_request_free(fc->destroy_req);
  467. mutex_destroy(&fc->inst_mutex);
  468. bdi_destroy(&fc->bdi);
  469. fc->release(fc);
  470. }
  471. }
  472. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
  473. {
  474. atomic_inc(&fc->count);
  475. return fc;
  476. }
  477. static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
  478. {
  479. struct fuse_attr attr;
  480. memset(&attr, 0, sizeof(attr));
  481. attr.mode = mode;
  482. attr.ino = FUSE_ROOT_ID;
  483. attr.nlink = 1;
  484. return fuse_iget(sb, 1, 0, &attr, 0, 0);
  485. }
  486. struct fuse_inode_handle {
  487. u64 nodeid;
  488. u32 generation;
  489. };
  490. static struct dentry *fuse_get_dentry(struct super_block *sb,
  491. struct fuse_inode_handle *handle)
  492. {
  493. struct fuse_conn *fc = get_fuse_conn_super(sb);
  494. struct inode *inode;
  495. struct dentry *entry;
  496. int err = -ESTALE;
  497. if (handle->nodeid == 0)
  498. goto out_err;
  499. inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
  500. if (!inode) {
  501. struct fuse_entry_out outarg;
  502. struct qstr name;
  503. if (!fc->export_support)
  504. goto out_err;
  505. name.len = 1;
  506. name.name = ".";
  507. err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
  508. &inode);
  509. if (err && err != -ENOENT)
  510. goto out_err;
  511. if (err || !inode) {
  512. err = -ESTALE;
  513. goto out_err;
  514. }
  515. err = -EIO;
  516. if (get_node_id(inode) != handle->nodeid)
  517. goto out_iput;
  518. }
  519. err = -ESTALE;
  520. if (inode->i_generation != handle->generation)
  521. goto out_iput;
  522. entry = d_obtain_alias(inode);
  523. if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) {
  524. entry->d_op = &fuse_dentry_operations;
  525. fuse_invalidate_entry_cache(entry);
  526. }
  527. return entry;
  528. out_iput:
  529. iput(inode);
  530. out_err:
  531. return ERR_PTR(err);
  532. }
  533. static int fuse_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
  534. int connectable)
  535. {
  536. struct inode *inode = dentry->d_inode;
  537. bool encode_parent = connectable && !S_ISDIR(inode->i_mode);
  538. int len = encode_parent ? 6 : 3;
  539. u64 nodeid;
  540. u32 generation;
  541. if (*max_len < len)
  542. return 255;
  543. nodeid = get_fuse_inode(inode)->nodeid;
  544. generation = inode->i_generation;
  545. fh[0] = (u32)(nodeid >> 32);
  546. fh[1] = (u32)(nodeid & 0xffffffff);
  547. fh[2] = generation;
  548. if (encode_parent) {
  549. struct inode *parent;
  550. spin_lock(&dentry->d_lock);
  551. parent = dentry->d_parent->d_inode;
  552. nodeid = get_fuse_inode(parent)->nodeid;
  553. generation = parent->i_generation;
  554. spin_unlock(&dentry->d_lock);
  555. fh[3] = (u32)(nodeid >> 32);
  556. fh[4] = (u32)(nodeid & 0xffffffff);
  557. fh[5] = generation;
  558. }
  559. *max_len = len;
  560. return encode_parent ? 0x82 : 0x81;
  561. }
  562. static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
  563. struct fid *fid, int fh_len, int fh_type)
  564. {
  565. struct fuse_inode_handle handle;
  566. if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
  567. return NULL;
  568. handle.nodeid = (u64) fid->raw[0] << 32;
  569. handle.nodeid |= (u64) fid->raw[1];
  570. handle.generation = fid->raw[2];
  571. return fuse_get_dentry(sb, &handle);
  572. }
  573. static struct dentry *fuse_fh_to_parent(struct super_block *sb,
  574. struct fid *fid, int fh_len, int fh_type)
  575. {
  576. struct fuse_inode_handle parent;
  577. if (fh_type != 0x82 || fh_len < 6)
  578. return NULL;
  579. parent.nodeid = (u64) fid->raw[3] << 32;
  580. parent.nodeid |= (u64) fid->raw[4];
  581. parent.generation = fid->raw[5];
  582. return fuse_get_dentry(sb, &parent);
  583. }
  584. static struct dentry *fuse_get_parent(struct dentry *child)
  585. {
  586. struct inode *child_inode = child->d_inode;
  587. struct fuse_conn *fc = get_fuse_conn(child_inode);
  588. struct inode *inode;
  589. struct dentry *parent;
  590. struct fuse_entry_out outarg;
  591. struct qstr name;
  592. int err;
  593. if (!fc->export_support)
  594. return ERR_PTR(-ESTALE);
  595. name.len = 2;
  596. name.name = "..";
  597. err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
  598. &name, &outarg, &inode);
  599. if (err) {
  600. if (err == -ENOENT)
  601. return ERR_PTR(-ESTALE);
  602. return ERR_PTR(err);
  603. }
  604. parent = d_obtain_alias(inode);
  605. if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) {
  606. parent->d_op = &fuse_dentry_operations;
  607. fuse_invalidate_entry_cache(parent);
  608. }
  609. return parent;
  610. }
  611. static const struct export_operations fuse_export_operations = {
  612. .fh_to_dentry = fuse_fh_to_dentry,
  613. .fh_to_parent = fuse_fh_to_parent,
  614. .encode_fh = fuse_encode_fh,
  615. .get_parent = fuse_get_parent,
  616. };
  617. static const struct super_operations fuse_super_operations = {
  618. .alloc_inode = fuse_alloc_inode,
  619. .destroy_inode = fuse_destroy_inode,
  620. .clear_inode = fuse_clear_inode,
  621. .drop_inode = generic_delete_inode,
  622. .remount_fs = fuse_remount_fs,
  623. .put_super = fuse_put_super,
  624. .umount_begin = fuse_umount_begin,
  625. .statfs = fuse_statfs,
  626. .show_options = fuse_show_options,
  627. };
  628. static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
  629. {
  630. struct fuse_init_out *arg = &req->misc.init_out;
  631. if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
  632. fc->conn_error = 1;
  633. else {
  634. unsigned long ra_pages;
  635. if (arg->minor >= 6) {
  636. ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
  637. if (arg->flags & FUSE_ASYNC_READ)
  638. fc->async_read = 1;
  639. if (!(arg->flags & FUSE_POSIX_LOCKS))
  640. fc->no_lock = 1;
  641. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  642. fc->atomic_o_trunc = 1;
  643. if (arg->minor >= 9) {
  644. /* LOOKUP has dependency on proto version */
  645. if (arg->flags & FUSE_EXPORT_SUPPORT)
  646. fc->export_support = 1;
  647. }
  648. if (arg->flags & FUSE_BIG_WRITES)
  649. fc->big_writes = 1;
  650. } else {
  651. ra_pages = fc->max_read / PAGE_CACHE_SIZE;
  652. fc->no_lock = 1;
  653. }
  654. fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
  655. fc->minor = arg->minor;
  656. fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
  657. fc->max_write = max_t(unsigned, 4096, fc->max_write);
  658. fc->conn_init = 1;
  659. }
  660. fc->blocked = 0;
  661. wake_up_all(&fc->blocked_waitq);
  662. }
  663. static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
  664. {
  665. struct fuse_init_in *arg = &req->misc.init_in;
  666. arg->major = FUSE_KERNEL_VERSION;
  667. arg->minor = FUSE_KERNEL_MINOR_VERSION;
  668. arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
  669. arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
  670. FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES;
  671. req->in.h.opcode = FUSE_INIT;
  672. req->in.numargs = 1;
  673. req->in.args[0].size = sizeof(*arg);
  674. req->in.args[0].value = arg;
  675. req->out.numargs = 1;
  676. /* Variable length arguement used for backward compatibility
  677. with interface version < 7.5. Rest of init_out is zeroed
  678. by do_get_request(), so a short reply is not a problem */
  679. req->out.argvar = 1;
  680. req->out.args[0].size = sizeof(struct fuse_init_out);
  681. req->out.args[0].value = &req->misc.init_out;
  682. req->end = process_init_reply;
  683. fuse_request_send_background(fc, req);
  684. }
  685. static void fuse_free_conn(struct fuse_conn *fc)
  686. {
  687. kfree(fc);
  688. }
  689. static int fuse_fill_super(struct super_block *sb, void *data, int silent)
  690. {
  691. struct fuse_conn *fc;
  692. struct inode *root;
  693. struct fuse_mount_data d;
  694. struct file *file;
  695. struct dentry *root_dentry;
  696. struct fuse_req *init_req;
  697. int err;
  698. int is_bdev = sb->s_bdev != NULL;
  699. if (sb->s_flags & MS_MANDLOCK)
  700. return -EINVAL;
  701. if (!parse_fuse_opt((char *) data, &d, is_bdev))
  702. return -EINVAL;
  703. if (is_bdev) {
  704. #ifdef CONFIG_BLOCK
  705. if (!sb_set_blocksize(sb, d.blksize))
  706. return -EINVAL;
  707. #endif
  708. } else {
  709. sb->s_blocksize = PAGE_CACHE_SIZE;
  710. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  711. }
  712. sb->s_magic = FUSE_SUPER_MAGIC;
  713. sb->s_op = &fuse_super_operations;
  714. sb->s_maxbytes = MAX_LFS_FILESIZE;
  715. sb->s_export_op = &fuse_export_operations;
  716. file = fget(d.fd);
  717. if (!file)
  718. return -EINVAL;
  719. if (file->f_op != &fuse_dev_operations)
  720. return -EINVAL;
  721. fc = kmalloc(sizeof(*fc), GFP_KERNEL);
  722. if (!fc)
  723. return -ENOMEM;
  724. err = fuse_conn_init(fc, sb);
  725. if (err) {
  726. kfree(fc);
  727. return err;
  728. }
  729. fc->release = fuse_free_conn;
  730. fc->flags = d.flags;
  731. fc->user_id = d.user_id;
  732. fc->group_id = d.group_id;
  733. fc->max_read = max_t(unsigned, 4096, d.max_read);
  734. /* Used by get_root_inode() */
  735. sb->s_fs_info = fc;
  736. err = -ENOMEM;
  737. root = fuse_get_root_inode(sb, d.rootmode);
  738. if (!root)
  739. goto err;
  740. root_dentry = d_alloc_root(root);
  741. if (!root_dentry) {
  742. iput(root);
  743. goto err;
  744. }
  745. init_req = fuse_request_alloc();
  746. if (!init_req)
  747. goto err_put_root;
  748. if (is_bdev) {
  749. fc->destroy_req = fuse_request_alloc();
  750. if (!fc->destroy_req)
  751. goto err_free_init_req;
  752. }
  753. mutex_lock(&fuse_mutex);
  754. err = -EINVAL;
  755. if (file->private_data)
  756. goto err_unlock;
  757. err = fuse_ctl_add_conn(fc);
  758. if (err)
  759. goto err_unlock;
  760. list_add_tail(&fc->entry, &fuse_conn_list);
  761. sb->s_root = root_dentry;
  762. fc->connected = 1;
  763. file->private_data = fuse_conn_get(fc);
  764. mutex_unlock(&fuse_mutex);
  765. /*
  766. * atomic_dec_and_test() in fput() provides the necessary
  767. * memory barrier for file->private_data to be visible on all
  768. * CPUs after this
  769. */
  770. fput(file);
  771. fuse_send_init(fc, init_req);
  772. return 0;
  773. err_unlock:
  774. mutex_unlock(&fuse_mutex);
  775. err_free_init_req:
  776. fuse_request_free(init_req);
  777. err_put_root:
  778. dput(root_dentry);
  779. err:
  780. fput(file);
  781. fuse_conn_put(fc);
  782. return err;
  783. }
  784. static int fuse_get_sb(struct file_system_type *fs_type,
  785. int flags, const char *dev_name,
  786. void *raw_data, struct vfsmount *mnt)
  787. {
  788. return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
  789. }
  790. static struct file_system_type fuse_fs_type = {
  791. .owner = THIS_MODULE,
  792. .name = "fuse",
  793. .fs_flags = FS_HAS_SUBTYPE,
  794. .get_sb = fuse_get_sb,
  795. .kill_sb = kill_anon_super,
  796. };
  797. #ifdef CONFIG_BLOCK
  798. static int fuse_get_sb_blk(struct file_system_type *fs_type,
  799. int flags, const char *dev_name,
  800. void *raw_data, struct vfsmount *mnt)
  801. {
  802. return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super,
  803. mnt);
  804. }
  805. static struct file_system_type fuseblk_fs_type = {
  806. .owner = THIS_MODULE,
  807. .name = "fuseblk",
  808. .get_sb = fuse_get_sb_blk,
  809. .kill_sb = kill_block_super,
  810. .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
  811. };
  812. static inline int register_fuseblk(void)
  813. {
  814. return register_filesystem(&fuseblk_fs_type);
  815. }
  816. static inline void unregister_fuseblk(void)
  817. {
  818. unregister_filesystem(&fuseblk_fs_type);
  819. }
  820. #else
  821. static inline int register_fuseblk(void)
  822. {
  823. return 0;
  824. }
  825. static inline void unregister_fuseblk(void)
  826. {
  827. }
  828. #endif
  829. static void fuse_inode_init_once(void *foo)
  830. {
  831. struct inode *inode = foo;
  832. inode_init_once(inode);
  833. }
  834. static int __init fuse_fs_init(void)
  835. {
  836. int err;
  837. err = register_filesystem(&fuse_fs_type);
  838. if (err)
  839. goto out;
  840. err = register_fuseblk();
  841. if (err)
  842. goto out_unreg;
  843. fuse_inode_cachep = kmem_cache_create("fuse_inode",
  844. sizeof(struct fuse_inode),
  845. 0, SLAB_HWCACHE_ALIGN,
  846. fuse_inode_init_once);
  847. err = -ENOMEM;
  848. if (!fuse_inode_cachep)
  849. goto out_unreg2;
  850. return 0;
  851. out_unreg2:
  852. unregister_fuseblk();
  853. out_unreg:
  854. unregister_filesystem(&fuse_fs_type);
  855. out:
  856. return err;
  857. }
  858. static void fuse_fs_cleanup(void)
  859. {
  860. unregister_filesystem(&fuse_fs_type);
  861. unregister_fuseblk();
  862. kmem_cache_destroy(fuse_inode_cachep);
  863. }
  864. static struct kobject *fuse_kobj;
  865. static struct kobject *connections_kobj;
  866. static int fuse_sysfs_init(void)
  867. {
  868. int err;
  869. fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
  870. if (!fuse_kobj) {
  871. err = -ENOMEM;
  872. goto out_err;
  873. }
  874. connections_kobj = kobject_create_and_add("connections", fuse_kobj);
  875. if (!connections_kobj) {
  876. err = -ENOMEM;
  877. goto out_fuse_unregister;
  878. }
  879. return 0;
  880. out_fuse_unregister:
  881. kobject_put(fuse_kobj);
  882. out_err:
  883. return err;
  884. }
  885. static void fuse_sysfs_cleanup(void)
  886. {
  887. kobject_put(connections_kobj);
  888. kobject_put(fuse_kobj);
  889. }
  890. static int __init fuse_init(void)
  891. {
  892. int res;
  893. printk(KERN_INFO "fuse init (API version %i.%i)\n",
  894. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  895. INIT_LIST_HEAD(&fuse_conn_list);
  896. res = fuse_fs_init();
  897. if (res)
  898. goto err;
  899. res = fuse_dev_init();
  900. if (res)
  901. goto err_fs_cleanup;
  902. res = fuse_sysfs_init();
  903. if (res)
  904. goto err_dev_cleanup;
  905. res = fuse_ctl_init();
  906. if (res)
  907. goto err_sysfs_cleanup;
  908. return 0;
  909. err_sysfs_cleanup:
  910. fuse_sysfs_cleanup();
  911. err_dev_cleanup:
  912. fuse_dev_cleanup();
  913. err_fs_cleanup:
  914. fuse_fs_cleanup();
  915. err:
  916. return res;
  917. }
  918. static void __exit fuse_exit(void)
  919. {
  920. printk(KERN_DEBUG "fuse exit\n");
  921. fuse_ctl_cleanup();
  922. fuse_sysfs_cleanup();
  923. fuse_fs_cleanup();
  924. fuse_dev_cleanup();
  925. }
  926. module_init(fuse_init);
  927. module_exit(fuse_exit);