inode.c 28 KB

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