inode.c 27 KB

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