inode.c 29 KB

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