dir.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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/file.h>
  10. #include <linux/sched.h>
  11. #include <linux/namei.h>
  12. #include <linux/slab.h>
  13. #include <linux/xattr.h>
  14. #include <linux/posix_acl.h>
  15. static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
  16. {
  17. struct fuse_conn *fc = get_fuse_conn(dir);
  18. struct fuse_inode *fi = get_fuse_inode(dir);
  19. if (!fc->do_readdirplus)
  20. return false;
  21. if (!fc->readdirplus_auto)
  22. return true;
  23. if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
  24. return true;
  25. if (ctx->pos == 0)
  26. return true;
  27. return false;
  28. }
  29. static void fuse_advise_use_readdirplus(struct inode *dir)
  30. {
  31. struct fuse_inode *fi = get_fuse_inode(dir);
  32. set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
  33. }
  34. union fuse_dentry {
  35. u64 time;
  36. struct rcu_head rcu;
  37. };
  38. static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
  39. {
  40. ((union fuse_dentry *) entry->d_fsdata)->time = time;
  41. }
  42. static inline u64 fuse_dentry_time(struct dentry *entry)
  43. {
  44. return ((union fuse_dentry *) entry->d_fsdata)->time;
  45. }
  46. /*
  47. * FUSE caches dentries and attributes with separate timeout. The
  48. * time in jiffies until the dentry/attributes are valid is stored in
  49. * dentry->d_fsdata and fuse_inode->i_time respectively.
  50. */
  51. /*
  52. * Calculate the time in jiffies until a dentry/attributes are valid
  53. */
  54. static u64 time_to_jiffies(u64 sec, u32 nsec)
  55. {
  56. if (sec || nsec) {
  57. struct timespec64 ts = {
  58. sec,
  59. min_t(u32, nsec, NSEC_PER_SEC - 1)
  60. };
  61. return get_jiffies_64() + timespec64_to_jiffies(&ts);
  62. } else
  63. return 0;
  64. }
  65. /*
  66. * Set dentry and possibly attribute timeouts from the lookup/mk*
  67. * replies
  68. */
  69. static void fuse_change_entry_timeout(struct dentry *entry,
  70. struct fuse_entry_out *o)
  71. {
  72. fuse_dentry_settime(entry,
  73. time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
  74. }
  75. static u64 attr_timeout(struct fuse_attr_out *o)
  76. {
  77. return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
  78. }
  79. static u64 entry_attr_timeout(struct fuse_entry_out *o)
  80. {
  81. return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
  82. }
  83. /*
  84. * Mark the attributes as stale, so that at the next call to
  85. * ->getattr() they will be fetched from userspace
  86. */
  87. void fuse_invalidate_attr(struct inode *inode)
  88. {
  89. get_fuse_inode(inode)->i_time = 0;
  90. }
  91. /**
  92. * Mark the attributes as stale due to an atime change. Avoid the invalidate if
  93. * atime is not used.
  94. */
  95. void fuse_invalidate_atime(struct inode *inode)
  96. {
  97. if (!IS_RDONLY(inode))
  98. fuse_invalidate_attr(inode);
  99. }
  100. /*
  101. * Just mark the entry as stale, so that a next attempt to look it up
  102. * will result in a new lookup call to userspace
  103. *
  104. * This is called when a dentry is about to become negative and the
  105. * timeout is unknown (unlink, rmdir, rename and in some cases
  106. * lookup)
  107. */
  108. void fuse_invalidate_entry_cache(struct dentry *entry)
  109. {
  110. fuse_dentry_settime(entry, 0);
  111. }
  112. /*
  113. * Same as fuse_invalidate_entry_cache(), but also try to remove the
  114. * dentry from the hash
  115. */
  116. static void fuse_invalidate_entry(struct dentry *entry)
  117. {
  118. d_invalidate(entry);
  119. fuse_invalidate_entry_cache(entry);
  120. }
  121. static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args,
  122. u64 nodeid, const struct qstr *name,
  123. struct fuse_entry_out *outarg)
  124. {
  125. memset(outarg, 0, sizeof(struct fuse_entry_out));
  126. args->in.h.opcode = FUSE_LOOKUP;
  127. args->in.h.nodeid = nodeid;
  128. args->in.numargs = 1;
  129. args->in.args[0].size = name->len + 1;
  130. args->in.args[0].value = name->name;
  131. args->out.numargs = 1;
  132. args->out.args[0].size = sizeof(struct fuse_entry_out);
  133. args->out.args[0].value = outarg;
  134. }
  135. u64 fuse_get_attr_version(struct fuse_conn *fc)
  136. {
  137. u64 curr_version;
  138. /*
  139. * The spin lock isn't actually needed on 64bit archs, but we
  140. * don't yet care too much about such optimizations.
  141. */
  142. spin_lock(&fc->lock);
  143. curr_version = fc->attr_version;
  144. spin_unlock(&fc->lock);
  145. return curr_version;
  146. }
  147. /*
  148. * Check whether the dentry is still valid
  149. *
  150. * If the entry validity timeout has expired and the dentry is
  151. * positive, try to redo the lookup. If the lookup results in a
  152. * different inode, then let the VFS invalidate the dentry and redo
  153. * the lookup once more. If the lookup results in the same inode,
  154. * then refresh the attributes, timeouts and mark the dentry valid.
  155. */
  156. static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
  157. {
  158. struct inode *inode;
  159. struct dentry *parent;
  160. struct fuse_conn *fc;
  161. struct fuse_inode *fi;
  162. int ret;
  163. inode = d_inode_rcu(entry);
  164. if (inode && is_bad_inode(inode))
  165. goto invalid;
  166. else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
  167. (flags & LOOKUP_REVAL)) {
  168. struct fuse_entry_out outarg;
  169. FUSE_ARGS(args);
  170. struct fuse_forget_link *forget;
  171. u64 attr_version;
  172. /* For negative dentries, always do a fresh lookup */
  173. if (!inode)
  174. goto invalid;
  175. ret = -ECHILD;
  176. if (flags & LOOKUP_RCU)
  177. goto out;
  178. fc = get_fuse_conn(inode);
  179. forget = fuse_alloc_forget();
  180. ret = -ENOMEM;
  181. if (!forget)
  182. goto out;
  183. attr_version = fuse_get_attr_version(fc);
  184. parent = dget_parent(entry);
  185. fuse_lookup_init(fc, &args, get_node_id(d_inode(parent)),
  186. &entry->d_name, &outarg);
  187. ret = fuse_simple_request(fc, &args);
  188. dput(parent);
  189. /* Zero nodeid is same as -ENOENT */
  190. if (!ret && !outarg.nodeid)
  191. ret = -ENOENT;
  192. if (!ret) {
  193. fi = get_fuse_inode(inode);
  194. if (outarg.nodeid != get_node_id(inode)) {
  195. fuse_queue_forget(fc, forget, outarg.nodeid, 1);
  196. goto invalid;
  197. }
  198. spin_lock(&fc->lock);
  199. fi->nlookup++;
  200. spin_unlock(&fc->lock);
  201. }
  202. kfree(forget);
  203. if (ret == -ENOMEM)
  204. goto out;
  205. if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
  206. goto invalid;
  207. forget_all_cached_acls(inode);
  208. fuse_change_attributes(inode, &outarg.attr,
  209. entry_attr_timeout(&outarg),
  210. attr_version);
  211. fuse_change_entry_timeout(entry, &outarg);
  212. } else if (inode) {
  213. fi = get_fuse_inode(inode);
  214. if (flags & LOOKUP_RCU) {
  215. if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
  216. return -ECHILD;
  217. } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
  218. parent = dget_parent(entry);
  219. fuse_advise_use_readdirplus(d_inode(parent));
  220. dput(parent);
  221. }
  222. }
  223. ret = 1;
  224. out:
  225. return ret;
  226. invalid:
  227. ret = 0;
  228. goto out;
  229. }
  230. static int invalid_nodeid(u64 nodeid)
  231. {
  232. return !nodeid || nodeid == FUSE_ROOT_ID;
  233. }
  234. static int fuse_dentry_init(struct dentry *dentry)
  235. {
  236. dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry), GFP_KERNEL);
  237. return dentry->d_fsdata ? 0 : -ENOMEM;
  238. }
  239. static void fuse_dentry_release(struct dentry *dentry)
  240. {
  241. union fuse_dentry *fd = dentry->d_fsdata;
  242. kfree_rcu(fd, rcu);
  243. }
  244. const struct dentry_operations fuse_dentry_operations = {
  245. .d_revalidate = fuse_dentry_revalidate,
  246. .d_init = fuse_dentry_init,
  247. .d_release = fuse_dentry_release,
  248. };
  249. const struct dentry_operations fuse_root_dentry_operations = {
  250. .d_init = fuse_dentry_init,
  251. .d_release = fuse_dentry_release,
  252. };
  253. int fuse_valid_type(int m)
  254. {
  255. return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
  256. S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
  257. }
  258. int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
  259. struct fuse_entry_out *outarg, struct inode **inode)
  260. {
  261. struct fuse_conn *fc = get_fuse_conn_super(sb);
  262. FUSE_ARGS(args);
  263. struct fuse_forget_link *forget;
  264. u64 attr_version;
  265. int err;
  266. *inode = NULL;
  267. err = -ENAMETOOLONG;
  268. if (name->len > FUSE_NAME_MAX)
  269. goto out;
  270. forget = fuse_alloc_forget();
  271. err = -ENOMEM;
  272. if (!forget)
  273. goto out;
  274. attr_version = fuse_get_attr_version(fc);
  275. fuse_lookup_init(fc, &args, nodeid, name, outarg);
  276. err = fuse_simple_request(fc, &args);
  277. /* Zero nodeid is same as -ENOENT, but with valid timeout */
  278. if (err || !outarg->nodeid)
  279. goto out_put_forget;
  280. err = -EIO;
  281. if (!outarg->nodeid)
  282. goto out_put_forget;
  283. if (!fuse_valid_type(outarg->attr.mode))
  284. goto out_put_forget;
  285. *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
  286. &outarg->attr, entry_attr_timeout(outarg),
  287. attr_version);
  288. err = -ENOMEM;
  289. if (!*inode) {
  290. fuse_queue_forget(fc, forget, outarg->nodeid, 1);
  291. goto out;
  292. }
  293. err = 0;
  294. out_put_forget:
  295. kfree(forget);
  296. out:
  297. return err;
  298. }
  299. static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
  300. unsigned int flags)
  301. {
  302. int err;
  303. struct fuse_entry_out outarg;
  304. struct inode *inode;
  305. struct dentry *newent;
  306. bool outarg_valid = true;
  307. fuse_lock_inode(dir);
  308. err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
  309. &outarg, &inode);
  310. fuse_unlock_inode(dir);
  311. if (err == -ENOENT) {
  312. outarg_valid = false;
  313. err = 0;
  314. }
  315. if (err)
  316. goto out_err;
  317. err = -EIO;
  318. if (inode && get_node_id(inode) == FUSE_ROOT_ID)
  319. goto out_iput;
  320. newent = d_splice_alias(inode, entry);
  321. err = PTR_ERR(newent);
  322. if (IS_ERR(newent))
  323. goto out_err;
  324. entry = newent ? newent : entry;
  325. if (outarg_valid)
  326. fuse_change_entry_timeout(entry, &outarg);
  327. else
  328. fuse_invalidate_entry_cache(entry);
  329. fuse_advise_use_readdirplus(dir);
  330. return newent;
  331. out_iput:
  332. iput(inode);
  333. out_err:
  334. return ERR_PTR(err);
  335. }
  336. /*
  337. * Atomic create+open operation
  338. *
  339. * If the filesystem doesn't support this, then fall back to separate
  340. * 'mknod' + 'open' requests.
  341. */
  342. static int fuse_create_open(struct inode *dir, struct dentry *entry,
  343. struct file *file, unsigned flags,
  344. umode_t mode, int *opened)
  345. {
  346. int err;
  347. struct inode *inode;
  348. struct fuse_conn *fc = get_fuse_conn(dir);
  349. FUSE_ARGS(args);
  350. struct fuse_forget_link *forget;
  351. struct fuse_create_in inarg;
  352. struct fuse_open_out outopen;
  353. struct fuse_entry_out outentry;
  354. struct fuse_file *ff;
  355. /* Userspace expects S_IFREG in create mode */
  356. BUG_ON((mode & S_IFMT) != S_IFREG);
  357. forget = fuse_alloc_forget();
  358. err = -ENOMEM;
  359. if (!forget)
  360. goto out_err;
  361. err = -ENOMEM;
  362. ff = fuse_file_alloc(fc);
  363. if (!ff)
  364. goto out_put_forget_req;
  365. if (!fc->dont_mask)
  366. mode &= ~current_umask();
  367. flags &= ~O_NOCTTY;
  368. memset(&inarg, 0, sizeof(inarg));
  369. memset(&outentry, 0, sizeof(outentry));
  370. inarg.flags = flags;
  371. inarg.mode = mode;
  372. inarg.umask = current_umask();
  373. args.in.h.opcode = FUSE_CREATE;
  374. args.in.h.nodeid = get_node_id(dir);
  375. args.in.numargs = 2;
  376. args.in.args[0].size = sizeof(inarg);
  377. args.in.args[0].value = &inarg;
  378. args.in.args[1].size = entry->d_name.len + 1;
  379. args.in.args[1].value = entry->d_name.name;
  380. args.out.numargs = 2;
  381. args.out.args[0].size = sizeof(outentry);
  382. args.out.args[0].value = &outentry;
  383. args.out.args[1].size = sizeof(outopen);
  384. args.out.args[1].value = &outopen;
  385. err = fuse_simple_request(fc, &args);
  386. if (err)
  387. goto out_free_ff;
  388. err = -EIO;
  389. if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
  390. goto out_free_ff;
  391. ff->fh = outopen.fh;
  392. ff->nodeid = outentry.nodeid;
  393. ff->open_flags = outopen.open_flags;
  394. inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
  395. &outentry.attr, entry_attr_timeout(&outentry), 0);
  396. if (!inode) {
  397. flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
  398. fuse_sync_release(ff, flags);
  399. fuse_queue_forget(fc, forget, outentry.nodeid, 1);
  400. err = -ENOMEM;
  401. goto out_err;
  402. }
  403. kfree(forget);
  404. d_instantiate(entry, inode);
  405. fuse_change_entry_timeout(entry, &outentry);
  406. fuse_invalidate_attr(dir);
  407. err = finish_open(file, entry, generic_file_open, opened);
  408. if (err) {
  409. fuse_sync_release(ff, flags);
  410. } else {
  411. file->private_data = fuse_file_get(ff);
  412. fuse_finish_open(inode, file);
  413. }
  414. return err;
  415. out_free_ff:
  416. fuse_file_free(ff);
  417. out_put_forget_req:
  418. kfree(forget);
  419. out_err:
  420. return err;
  421. }
  422. static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
  423. static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
  424. struct file *file, unsigned flags,
  425. umode_t mode, int *opened)
  426. {
  427. int err;
  428. struct fuse_conn *fc = get_fuse_conn(dir);
  429. struct dentry *res = NULL;
  430. if (d_in_lookup(entry)) {
  431. res = fuse_lookup(dir, entry, 0);
  432. if (IS_ERR(res))
  433. return PTR_ERR(res);
  434. if (res)
  435. entry = res;
  436. }
  437. if (!(flags & O_CREAT) || d_really_is_positive(entry))
  438. goto no_open;
  439. /* Only creates */
  440. *opened |= FILE_CREATED;
  441. if (fc->no_create)
  442. goto mknod;
  443. err = fuse_create_open(dir, entry, file, flags, mode, opened);
  444. if (err == -ENOSYS) {
  445. fc->no_create = 1;
  446. goto mknod;
  447. }
  448. out_dput:
  449. dput(res);
  450. return err;
  451. mknod:
  452. err = fuse_mknod(dir, entry, mode, 0);
  453. if (err)
  454. goto out_dput;
  455. no_open:
  456. return finish_no_open(file, res);
  457. }
  458. /*
  459. * Code shared between mknod, mkdir, symlink and link
  460. */
  461. static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
  462. struct inode *dir, struct dentry *entry,
  463. umode_t mode)
  464. {
  465. struct fuse_entry_out outarg;
  466. struct inode *inode;
  467. int err;
  468. struct fuse_forget_link *forget;
  469. forget = fuse_alloc_forget();
  470. if (!forget)
  471. return -ENOMEM;
  472. memset(&outarg, 0, sizeof(outarg));
  473. args->in.h.nodeid = get_node_id(dir);
  474. args->out.numargs = 1;
  475. args->out.args[0].size = sizeof(outarg);
  476. args->out.args[0].value = &outarg;
  477. err = fuse_simple_request(fc, args);
  478. if (err)
  479. goto out_put_forget_req;
  480. err = -EIO;
  481. if (invalid_nodeid(outarg.nodeid))
  482. goto out_put_forget_req;
  483. if ((outarg.attr.mode ^ mode) & S_IFMT)
  484. goto out_put_forget_req;
  485. inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
  486. &outarg.attr, entry_attr_timeout(&outarg), 0);
  487. if (!inode) {
  488. fuse_queue_forget(fc, forget, outarg.nodeid, 1);
  489. return -ENOMEM;
  490. }
  491. kfree(forget);
  492. err = d_instantiate_no_diralias(entry, inode);
  493. if (err)
  494. return err;
  495. fuse_change_entry_timeout(entry, &outarg);
  496. fuse_invalidate_attr(dir);
  497. return 0;
  498. out_put_forget_req:
  499. kfree(forget);
  500. return err;
  501. }
  502. static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
  503. dev_t rdev)
  504. {
  505. struct fuse_mknod_in inarg;
  506. struct fuse_conn *fc = get_fuse_conn(dir);
  507. FUSE_ARGS(args);
  508. if (!fc->dont_mask)
  509. mode &= ~current_umask();
  510. memset(&inarg, 0, sizeof(inarg));
  511. inarg.mode = mode;
  512. inarg.rdev = new_encode_dev(rdev);
  513. inarg.umask = current_umask();
  514. args.in.h.opcode = FUSE_MKNOD;
  515. args.in.numargs = 2;
  516. args.in.args[0].size = sizeof(inarg);
  517. args.in.args[0].value = &inarg;
  518. args.in.args[1].size = entry->d_name.len + 1;
  519. args.in.args[1].value = entry->d_name.name;
  520. return create_new_entry(fc, &args, dir, entry, mode);
  521. }
  522. static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
  523. bool excl)
  524. {
  525. return fuse_mknod(dir, entry, mode, 0);
  526. }
  527. static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
  528. {
  529. struct fuse_mkdir_in inarg;
  530. struct fuse_conn *fc = get_fuse_conn(dir);
  531. FUSE_ARGS(args);
  532. if (!fc->dont_mask)
  533. mode &= ~current_umask();
  534. memset(&inarg, 0, sizeof(inarg));
  535. inarg.mode = mode;
  536. inarg.umask = current_umask();
  537. args.in.h.opcode = FUSE_MKDIR;
  538. args.in.numargs = 2;
  539. args.in.args[0].size = sizeof(inarg);
  540. args.in.args[0].value = &inarg;
  541. args.in.args[1].size = entry->d_name.len + 1;
  542. args.in.args[1].value = entry->d_name.name;
  543. return create_new_entry(fc, &args, dir, entry, S_IFDIR);
  544. }
  545. static int fuse_symlink(struct inode *dir, struct dentry *entry,
  546. const char *link)
  547. {
  548. struct fuse_conn *fc = get_fuse_conn(dir);
  549. unsigned len = strlen(link) + 1;
  550. FUSE_ARGS(args);
  551. args.in.h.opcode = FUSE_SYMLINK;
  552. args.in.numargs = 2;
  553. args.in.args[0].size = entry->d_name.len + 1;
  554. args.in.args[0].value = entry->d_name.name;
  555. args.in.args[1].size = len;
  556. args.in.args[1].value = link;
  557. return create_new_entry(fc, &args, dir, entry, S_IFLNK);
  558. }
  559. void fuse_update_ctime(struct inode *inode)
  560. {
  561. if (!IS_NOCMTIME(inode)) {
  562. inode->i_ctime = current_time(inode);
  563. mark_inode_dirty_sync(inode);
  564. }
  565. }
  566. static int fuse_unlink(struct inode *dir, struct dentry *entry)
  567. {
  568. int err;
  569. struct fuse_conn *fc = get_fuse_conn(dir);
  570. FUSE_ARGS(args);
  571. args.in.h.opcode = FUSE_UNLINK;
  572. args.in.h.nodeid = get_node_id(dir);
  573. args.in.numargs = 1;
  574. args.in.args[0].size = entry->d_name.len + 1;
  575. args.in.args[0].value = entry->d_name.name;
  576. err = fuse_simple_request(fc, &args);
  577. if (!err) {
  578. struct inode *inode = d_inode(entry);
  579. struct fuse_inode *fi = get_fuse_inode(inode);
  580. spin_lock(&fc->lock);
  581. fi->attr_version = ++fc->attr_version;
  582. /*
  583. * If i_nlink == 0 then unlink doesn't make sense, yet this can
  584. * happen if userspace filesystem is careless. It would be
  585. * difficult to enforce correct nlink usage so just ignore this
  586. * condition here
  587. */
  588. if (inode->i_nlink > 0)
  589. drop_nlink(inode);
  590. spin_unlock(&fc->lock);
  591. fuse_invalidate_attr(inode);
  592. fuse_invalidate_attr(dir);
  593. fuse_invalidate_entry_cache(entry);
  594. fuse_update_ctime(inode);
  595. } else if (err == -EINTR)
  596. fuse_invalidate_entry(entry);
  597. return err;
  598. }
  599. static int fuse_rmdir(struct inode *dir, struct dentry *entry)
  600. {
  601. int err;
  602. struct fuse_conn *fc = get_fuse_conn(dir);
  603. FUSE_ARGS(args);
  604. args.in.h.opcode = FUSE_RMDIR;
  605. args.in.h.nodeid = get_node_id(dir);
  606. args.in.numargs = 1;
  607. args.in.args[0].size = entry->d_name.len + 1;
  608. args.in.args[0].value = entry->d_name.name;
  609. err = fuse_simple_request(fc, &args);
  610. if (!err) {
  611. clear_nlink(d_inode(entry));
  612. fuse_invalidate_attr(dir);
  613. fuse_invalidate_entry_cache(entry);
  614. } else if (err == -EINTR)
  615. fuse_invalidate_entry(entry);
  616. return err;
  617. }
  618. static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
  619. struct inode *newdir, struct dentry *newent,
  620. unsigned int flags, int opcode, size_t argsize)
  621. {
  622. int err;
  623. struct fuse_rename2_in inarg;
  624. struct fuse_conn *fc = get_fuse_conn(olddir);
  625. FUSE_ARGS(args);
  626. memset(&inarg, 0, argsize);
  627. inarg.newdir = get_node_id(newdir);
  628. inarg.flags = flags;
  629. args.in.h.opcode = opcode;
  630. args.in.h.nodeid = get_node_id(olddir);
  631. args.in.numargs = 3;
  632. args.in.args[0].size = argsize;
  633. args.in.args[0].value = &inarg;
  634. args.in.args[1].size = oldent->d_name.len + 1;
  635. args.in.args[1].value = oldent->d_name.name;
  636. args.in.args[2].size = newent->d_name.len + 1;
  637. args.in.args[2].value = newent->d_name.name;
  638. err = fuse_simple_request(fc, &args);
  639. if (!err) {
  640. /* ctime changes */
  641. fuse_invalidate_attr(d_inode(oldent));
  642. fuse_update_ctime(d_inode(oldent));
  643. if (flags & RENAME_EXCHANGE) {
  644. fuse_invalidate_attr(d_inode(newent));
  645. fuse_update_ctime(d_inode(newent));
  646. }
  647. fuse_invalidate_attr(olddir);
  648. if (olddir != newdir)
  649. fuse_invalidate_attr(newdir);
  650. /* newent will end up negative */
  651. if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
  652. fuse_invalidate_attr(d_inode(newent));
  653. fuse_invalidate_entry_cache(newent);
  654. fuse_update_ctime(d_inode(newent));
  655. }
  656. } else if (err == -EINTR) {
  657. /* If request was interrupted, DEITY only knows if the
  658. rename actually took place. If the invalidation
  659. fails (e.g. some process has CWD under the renamed
  660. directory), then there can be inconsistency between
  661. the dcache and the real filesystem. Tough luck. */
  662. fuse_invalidate_entry(oldent);
  663. if (d_really_is_positive(newent))
  664. fuse_invalidate_entry(newent);
  665. }
  666. return err;
  667. }
  668. static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
  669. struct inode *newdir, struct dentry *newent,
  670. unsigned int flags)
  671. {
  672. struct fuse_conn *fc = get_fuse_conn(olddir);
  673. int err;
  674. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  675. return -EINVAL;
  676. if (flags) {
  677. if (fc->no_rename2 || fc->minor < 23)
  678. return -EINVAL;
  679. err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
  680. FUSE_RENAME2,
  681. sizeof(struct fuse_rename2_in));
  682. if (err == -ENOSYS) {
  683. fc->no_rename2 = 1;
  684. err = -EINVAL;
  685. }
  686. } else {
  687. err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
  688. FUSE_RENAME,
  689. sizeof(struct fuse_rename_in));
  690. }
  691. return err;
  692. }
  693. static int fuse_link(struct dentry *entry, struct inode *newdir,
  694. struct dentry *newent)
  695. {
  696. int err;
  697. struct fuse_link_in inarg;
  698. struct inode *inode = d_inode(entry);
  699. struct fuse_conn *fc = get_fuse_conn(inode);
  700. FUSE_ARGS(args);
  701. memset(&inarg, 0, sizeof(inarg));
  702. inarg.oldnodeid = get_node_id(inode);
  703. args.in.h.opcode = FUSE_LINK;
  704. args.in.numargs = 2;
  705. args.in.args[0].size = sizeof(inarg);
  706. args.in.args[0].value = &inarg;
  707. args.in.args[1].size = newent->d_name.len + 1;
  708. args.in.args[1].value = newent->d_name.name;
  709. err = create_new_entry(fc, &args, newdir, newent, inode->i_mode);
  710. /* Contrary to "normal" filesystems it can happen that link
  711. makes two "logical" inodes point to the same "physical"
  712. inode. We invalidate the attributes of the old one, so it
  713. will reflect changes in the backing inode (link count,
  714. etc.)
  715. */
  716. if (!err) {
  717. struct fuse_inode *fi = get_fuse_inode(inode);
  718. spin_lock(&fc->lock);
  719. fi->attr_version = ++fc->attr_version;
  720. inc_nlink(inode);
  721. spin_unlock(&fc->lock);
  722. fuse_invalidate_attr(inode);
  723. fuse_update_ctime(inode);
  724. } else if (err == -EINTR) {
  725. fuse_invalidate_attr(inode);
  726. }
  727. return err;
  728. }
  729. static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
  730. struct kstat *stat)
  731. {
  732. unsigned int blkbits;
  733. struct fuse_conn *fc = get_fuse_conn(inode);
  734. /* see the comment in fuse_change_attributes() */
  735. if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
  736. attr->size = i_size_read(inode);
  737. attr->mtime = inode->i_mtime.tv_sec;
  738. attr->mtimensec = inode->i_mtime.tv_nsec;
  739. attr->ctime = inode->i_ctime.tv_sec;
  740. attr->ctimensec = inode->i_ctime.tv_nsec;
  741. }
  742. stat->dev = inode->i_sb->s_dev;
  743. stat->ino = attr->ino;
  744. stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
  745. stat->nlink = attr->nlink;
  746. stat->uid = make_kuid(&init_user_ns, attr->uid);
  747. stat->gid = make_kgid(&init_user_ns, attr->gid);
  748. stat->rdev = inode->i_rdev;
  749. stat->atime.tv_sec = attr->atime;
  750. stat->atime.tv_nsec = attr->atimensec;
  751. stat->mtime.tv_sec = attr->mtime;
  752. stat->mtime.tv_nsec = attr->mtimensec;
  753. stat->ctime.tv_sec = attr->ctime;
  754. stat->ctime.tv_nsec = attr->ctimensec;
  755. stat->size = attr->size;
  756. stat->blocks = attr->blocks;
  757. if (attr->blksize != 0)
  758. blkbits = ilog2(attr->blksize);
  759. else
  760. blkbits = inode->i_sb->s_blocksize_bits;
  761. stat->blksize = 1 << blkbits;
  762. }
  763. static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
  764. struct file *file)
  765. {
  766. int err;
  767. struct fuse_getattr_in inarg;
  768. struct fuse_attr_out outarg;
  769. struct fuse_conn *fc = get_fuse_conn(inode);
  770. FUSE_ARGS(args);
  771. u64 attr_version;
  772. attr_version = fuse_get_attr_version(fc);
  773. memset(&inarg, 0, sizeof(inarg));
  774. memset(&outarg, 0, sizeof(outarg));
  775. /* Directories have separate file-handle space */
  776. if (file && S_ISREG(inode->i_mode)) {
  777. struct fuse_file *ff = file->private_data;
  778. inarg.getattr_flags |= FUSE_GETATTR_FH;
  779. inarg.fh = ff->fh;
  780. }
  781. args.in.h.opcode = FUSE_GETATTR;
  782. args.in.h.nodeid = get_node_id(inode);
  783. args.in.numargs = 1;
  784. args.in.args[0].size = sizeof(inarg);
  785. args.in.args[0].value = &inarg;
  786. args.out.numargs = 1;
  787. args.out.args[0].size = sizeof(outarg);
  788. args.out.args[0].value = &outarg;
  789. err = fuse_simple_request(fc, &args);
  790. if (!err) {
  791. if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
  792. make_bad_inode(inode);
  793. err = -EIO;
  794. } else {
  795. fuse_change_attributes(inode, &outarg.attr,
  796. attr_timeout(&outarg),
  797. attr_version);
  798. if (stat)
  799. fuse_fillattr(inode, &outarg.attr, stat);
  800. }
  801. }
  802. return err;
  803. }
  804. int fuse_update_attributes(struct inode *inode, struct kstat *stat,
  805. struct file *file, bool *refreshed)
  806. {
  807. struct fuse_inode *fi = get_fuse_inode(inode);
  808. int err;
  809. bool r;
  810. if (time_before64(fi->i_time, get_jiffies_64())) {
  811. r = true;
  812. forget_all_cached_acls(inode);
  813. err = fuse_do_getattr(inode, stat, file);
  814. } else {
  815. r = false;
  816. err = 0;
  817. if (stat) {
  818. generic_fillattr(inode, stat);
  819. stat->mode = fi->orig_i_mode;
  820. stat->ino = fi->orig_ino;
  821. }
  822. }
  823. if (refreshed != NULL)
  824. *refreshed = r;
  825. return err;
  826. }
  827. int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
  828. u64 child_nodeid, struct qstr *name)
  829. {
  830. int err = -ENOTDIR;
  831. struct inode *parent;
  832. struct dentry *dir;
  833. struct dentry *entry;
  834. parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
  835. if (!parent)
  836. return -ENOENT;
  837. inode_lock(parent);
  838. if (!S_ISDIR(parent->i_mode))
  839. goto unlock;
  840. err = -ENOENT;
  841. dir = d_find_alias(parent);
  842. if (!dir)
  843. goto unlock;
  844. name->hash = full_name_hash(dir, name->name, name->len);
  845. entry = d_lookup(dir, name);
  846. dput(dir);
  847. if (!entry)
  848. goto unlock;
  849. fuse_invalidate_attr(parent);
  850. fuse_invalidate_entry(entry);
  851. if (child_nodeid != 0 && d_really_is_positive(entry)) {
  852. inode_lock(d_inode(entry));
  853. if (get_node_id(d_inode(entry)) != child_nodeid) {
  854. err = -ENOENT;
  855. goto badentry;
  856. }
  857. if (d_mountpoint(entry)) {
  858. err = -EBUSY;
  859. goto badentry;
  860. }
  861. if (d_is_dir(entry)) {
  862. shrink_dcache_parent(entry);
  863. if (!simple_empty(entry)) {
  864. err = -ENOTEMPTY;
  865. goto badentry;
  866. }
  867. d_inode(entry)->i_flags |= S_DEAD;
  868. }
  869. dont_mount(entry);
  870. clear_nlink(d_inode(entry));
  871. err = 0;
  872. badentry:
  873. inode_unlock(d_inode(entry));
  874. if (!err)
  875. d_delete(entry);
  876. } else {
  877. err = 0;
  878. }
  879. dput(entry);
  880. unlock:
  881. inode_unlock(parent);
  882. iput(parent);
  883. return err;
  884. }
  885. /*
  886. * Calling into a user-controlled filesystem gives the filesystem
  887. * daemon ptrace-like capabilities over the current process. This
  888. * means, that the filesystem daemon is able to record the exact
  889. * filesystem operations performed, and can also control the behavior
  890. * of the requester process in otherwise impossible ways. For example
  891. * it can delay the operation for arbitrary length of time allowing
  892. * DoS against the requester.
  893. *
  894. * For this reason only those processes can call into the filesystem,
  895. * for which the owner of the mount has ptrace privilege. This
  896. * excludes processes started by other users, suid or sgid processes.
  897. */
  898. int fuse_allow_current_process(struct fuse_conn *fc)
  899. {
  900. const struct cred *cred;
  901. if (fc->allow_other)
  902. return 1;
  903. cred = current_cred();
  904. if (uid_eq(cred->euid, fc->user_id) &&
  905. uid_eq(cred->suid, fc->user_id) &&
  906. uid_eq(cred->uid, fc->user_id) &&
  907. gid_eq(cred->egid, fc->group_id) &&
  908. gid_eq(cred->sgid, fc->group_id) &&
  909. gid_eq(cred->gid, fc->group_id))
  910. return 1;
  911. return 0;
  912. }
  913. static int fuse_access(struct inode *inode, int mask)
  914. {
  915. struct fuse_conn *fc = get_fuse_conn(inode);
  916. FUSE_ARGS(args);
  917. struct fuse_access_in inarg;
  918. int err;
  919. BUG_ON(mask & MAY_NOT_BLOCK);
  920. if (fc->no_access)
  921. return 0;
  922. memset(&inarg, 0, sizeof(inarg));
  923. inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
  924. args.in.h.opcode = FUSE_ACCESS;
  925. args.in.h.nodeid = get_node_id(inode);
  926. args.in.numargs = 1;
  927. args.in.args[0].size = sizeof(inarg);
  928. args.in.args[0].value = &inarg;
  929. err = fuse_simple_request(fc, &args);
  930. if (err == -ENOSYS) {
  931. fc->no_access = 1;
  932. err = 0;
  933. }
  934. return err;
  935. }
  936. static int fuse_perm_getattr(struct inode *inode, int mask)
  937. {
  938. if (mask & MAY_NOT_BLOCK)
  939. return -ECHILD;
  940. forget_all_cached_acls(inode);
  941. return fuse_do_getattr(inode, NULL, NULL);
  942. }
  943. /*
  944. * Check permission. The two basic access models of FUSE are:
  945. *
  946. * 1) Local access checking ('default_permissions' mount option) based
  947. * on file mode. This is the plain old disk filesystem permission
  948. * modell.
  949. *
  950. * 2) "Remote" access checking, where server is responsible for
  951. * checking permission in each inode operation. An exception to this
  952. * is if ->permission() was invoked from sys_access() in which case an
  953. * access request is sent. Execute permission is still checked
  954. * locally based on file mode.
  955. */
  956. static int fuse_permission(struct inode *inode, int mask)
  957. {
  958. struct fuse_conn *fc = get_fuse_conn(inode);
  959. bool refreshed = false;
  960. int err = 0;
  961. if (!fuse_allow_current_process(fc))
  962. return -EACCES;
  963. /*
  964. * If attributes are needed, refresh them before proceeding
  965. */
  966. if (fc->default_permissions ||
  967. ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
  968. struct fuse_inode *fi = get_fuse_inode(inode);
  969. if (time_before64(fi->i_time, get_jiffies_64())) {
  970. refreshed = true;
  971. err = fuse_perm_getattr(inode, mask);
  972. if (err)
  973. return err;
  974. }
  975. }
  976. if (fc->default_permissions) {
  977. err = generic_permission(inode, mask);
  978. /* If permission is denied, try to refresh file
  979. attributes. This is also needed, because the root
  980. node will at first have no permissions */
  981. if (err == -EACCES && !refreshed) {
  982. err = fuse_perm_getattr(inode, mask);
  983. if (!err)
  984. err = generic_permission(inode, mask);
  985. }
  986. /* Note: the opposite of the above test does not
  987. exist. So if permissions are revoked this won't be
  988. noticed immediately, only after the attribute
  989. timeout has expired */
  990. } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
  991. err = fuse_access(inode, mask);
  992. } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  993. if (!(inode->i_mode & S_IXUGO)) {
  994. if (refreshed)
  995. return -EACCES;
  996. err = fuse_perm_getattr(inode, mask);
  997. if (!err && !(inode->i_mode & S_IXUGO))
  998. return -EACCES;
  999. }
  1000. }
  1001. return err;
  1002. }
  1003. static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
  1004. struct dir_context *ctx)
  1005. {
  1006. while (nbytes >= FUSE_NAME_OFFSET) {
  1007. struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
  1008. size_t reclen = FUSE_DIRENT_SIZE(dirent);
  1009. if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
  1010. return -EIO;
  1011. if (reclen > nbytes)
  1012. break;
  1013. if (memchr(dirent->name, '/', dirent->namelen) != NULL)
  1014. return -EIO;
  1015. if (!dir_emit(ctx, dirent->name, dirent->namelen,
  1016. dirent->ino, dirent->type))
  1017. break;
  1018. buf += reclen;
  1019. nbytes -= reclen;
  1020. ctx->pos = dirent->off;
  1021. }
  1022. return 0;
  1023. }
  1024. static int fuse_direntplus_link(struct file *file,
  1025. struct fuse_direntplus *direntplus,
  1026. u64 attr_version)
  1027. {
  1028. struct fuse_entry_out *o = &direntplus->entry_out;
  1029. struct fuse_dirent *dirent = &direntplus->dirent;
  1030. struct dentry *parent = file->f_path.dentry;
  1031. struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
  1032. struct dentry *dentry;
  1033. struct dentry *alias;
  1034. struct inode *dir = d_inode(parent);
  1035. struct fuse_conn *fc;
  1036. struct inode *inode;
  1037. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  1038. if (!o->nodeid) {
  1039. /*
  1040. * Unlike in the case of fuse_lookup, zero nodeid does not mean
  1041. * ENOENT. Instead, it only means the userspace filesystem did
  1042. * not want to return attributes/handle for this entry.
  1043. *
  1044. * So do nothing.
  1045. */
  1046. return 0;
  1047. }
  1048. if (name.name[0] == '.') {
  1049. /*
  1050. * We could potentially refresh the attributes of the directory
  1051. * and its parent?
  1052. */
  1053. if (name.len == 1)
  1054. return 0;
  1055. if (name.name[1] == '.' && name.len == 2)
  1056. return 0;
  1057. }
  1058. if (invalid_nodeid(o->nodeid))
  1059. return -EIO;
  1060. if (!fuse_valid_type(o->attr.mode))
  1061. return -EIO;
  1062. fc = get_fuse_conn(dir);
  1063. name.hash = full_name_hash(parent, name.name, name.len);
  1064. dentry = d_lookup(parent, &name);
  1065. if (!dentry) {
  1066. retry:
  1067. dentry = d_alloc_parallel(parent, &name, &wq);
  1068. if (IS_ERR(dentry))
  1069. return PTR_ERR(dentry);
  1070. }
  1071. if (!d_in_lookup(dentry)) {
  1072. struct fuse_inode *fi;
  1073. inode = d_inode(dentry);
  1074. if (!inode ||
  1075. get_node_id(inode) != o->nodeid ||
  1076. ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
  1077. d_invalidate(dentry);
  1078. dput(dentry);
  1079. goto retry;
  1080. }
  1081. if (is_bad_inode(inode)) {
  1082. dput(dentry);
  1083. return -EIO;
  1084. }
  1085. fi = get_fuse_inode(inode);
  1086. spin_lock(&fc->lock);
  1087. fi->nlookup++;
  1088. spin_unlock(&fc->lock);
  1089. forget_all_cached_acls(inode);
  1090. fuse_change_attributes(inode, &o->attr,
  1091. entry_attr_timeout(o),
  1092. attr_version);
  1093. /*
  1094. * The other branch comes via fuse_iget()
  1095. * which bumps nlookup inside
  1096. */
  1097. } else {
  1098. inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
  1099. &o->attr, entry_attr_timeout(o),
  1100. attr_version);
  1101. if (!inode)
  1102. inode = ERR_PTR(-ENOMEM);
  1103. alias = d_splice_alias(inode, dentry);
  1104. d_lookup_done(dentry);
  1105. if (alias) {
  1106. dput(dentry);
  1107. dentry = alias;
  1108. }
  1109. if (IS_ERR(dentry))
  1110. return PTR_ERR(dentry);
  1111. }
  1112. if (fc->readdirplus_auto)
  1113. set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
  1114. fuse_change_entry_timeout(dentry, o);
  1115. dput(dentry);
  1116. return 0;
  1117. }
  1118. static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
  1119. struct dir_context *ctx, u64 attr_version)
  1120. {
  1121. struct fuse_direntplus *direntplus;
  1122. struct fuse_dirent *dirent;
  1123. size_t reclen;
  1124. int over = 0;
  1125. int ret;
  1126. while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
  1127. direntplus = (struct fuse_direntplus *) buf;
  1128. dirent = &direntplus->dirent;
  1129. reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
  1130. if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
  1131. return -EIO;
  1132. if (reclen > nbytes)
  1133. break;
  1134. if (memchr(dirent->name, '/', dirent->namelen) != NULL)
  1135. return -EIO;
  1136. if (!over) {
  1137. /* We fill entries into dstbuf only as much as
  1138. it can hold. But we still continue iterating
  1139. over remaining entries to link them. If not,
  1140. we need to send a FORGET for each of those
  1141. which we did not link.
  1142. */
  1143. over = !dir_emit(ctx, dirent->name, dirent->namelen,
  1144. dirent->ino, dirent->type);
  1145. if (!over)
  1146. ctx->pos = dirent->off;
  1147. }
  1148. buf += reclen;
  1149. nbytes -= reclen;
  1150. ret = fuse_direntplus_link(file, direntplus, attr_version);
  1151. if (ret)
  1152. fuse_force_forget(file, direntplus->entry_out.nodeid);
  1153. }
  1154. return 0;
  1155. }
  1156. static int fuse_readdir(struct file *file, struct dir_context *ctx)
  1157. {
  1158. int plus, err;
  1159. size_t nbytes;
  1160. struct page *page;
  1161. struct inode *inode = file_inode(file);
  1162. struct fuse_conn *fc = get_fuse_conn(inode);
  1163. struct fuse_req *req;
  1164. u64 attr_version = 0;
  1165. if (is_bad_inode(inode))
  1166. return -EIO;
  1167. req = fuse_get_req(fc, 1);
  1168. if (IS_ERR(req))
  1169. return PTR_ERR(req);
  1170. page = alloc_page(GFP_KERNEL);
  1171. if (!page) {
  1172. fuse_put_request(fc, req);
  1173. return -ENOMEM;
  1174. }
  1175. plus = fuse_use_readdirplus(inode, ctx);
  1176. req->out.argpages = 1;
  1177. req->num_pages = 1;
  1178. req->pages[0] = page;
  1179. req->page_descs[0].length = PAGE_SIZE;
  1180. if (plus) {
  1181. attr_version = fuse_get_attr_version(fc);
  1182. fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
  1183. FUSE_READDIRPLUS);
  1184. } else {
  1185. fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
  1186. FUSE_READDIR);
  1187. }
  1188. fuse_lock_inode(inode);
  1189. fuse_request_send(fc, req);
  1190. fuse_unlock_inode(inode);
  1191. nbytes = req->out.args[0].size;
  1192. err = req->out.h.error;
  1193. fuse_put_request(fc, req);
  1194. if (!err) {
  1195. if (plus) {
  1196. err = parse_dirplusfile(page_address(page), nbytes,
  1197. file, ctx,
  1198. attr_version);
  1199. } else {
  1200. err = parse_dirfile(page_address(page), nbytes, file,
  1201. ctx);
  1202. }
  1203. }
  1204. __free_page(page);
  1205. fuse_invalidate_atime(inode);
  1206. return err;
  1207. }
  1208. static const char *fuse_get_link(struct dentry *dentry,
  1209. struct inode *inode,
  1210. struct delayed_call *done)
  1211. {
  1212. struct fuse_conn *fc = get_fuse_conn(inode);
  1213. FUSE_ARGS(args);
  1214. char *link;
  1215. ssize_t ret;
  1216. if (!dentry)
  1217. return ERR_PTR(-ECHILD);
  1218. link = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1219. if (!link)
  1220. return ERR_PTR(-ENOMEM);
  1221. args.in.h.opcode = FUSE_READLINK;
  1222. args.in.h.nodeid = get_node_id(inode);
  1223. args.out.argvar = 1;
  1224. args.out.numargs = 1;
  1225. args.out.args[0].size = PAGE_SIZE - 1;
  1226. args.out.args[0].value = link;
  1227. ret = fuse_simple_request(fc, &args);
  1228. if (ret < 0) {
  1229. kfree(link);
  1230. link = ERR_PTR(ret);
  1231. } else {
  1232. link[ret] = '\0';
  1233. set_delayed_call(done, kfree_link, link);
  1234. }
  1235. fuse_invalidate_atime(inode);
  1236. return link;
  1237. }
  1238. static int fuse_dir_open(struct inode *inode, struct file *file)
  1239. {
  1240. return fuse_open_common(inode, file, true);
  1241. }
  1242. static int fuse_dir_release(struct inode *inode, struct file *file)
  1243. {
  1244. fuse_release_common(file, FUSE_RELEASEDIR);
  1245. return 0;
  1246. }
  1247. static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
  1248. int datasync)
  1249. {
  1250. return fuse_fsync_common(file, start, end, datasync, 1);
  1251. }
  1252. static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
  1253. unsigned long arg)
  1254. {
  1255. struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
  1256. /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
  1257. if (fc->minor < 18)
  1258. return -ENOTTY;
  1259. return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
  1260. }
  1261. static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
  1262. unsigned long arg)
  1263. {
  1264. struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
  1265. if (fc->minor < 18)
  1266. return -ENOTTY;
  1267. return fuse_ioctl_common(file, cmd, arg,
  1268. FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
  1269. }
  1270. static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
  1271. {
  1272. /* Always update if mtime is explicitly set */
  1273. if (ivalid & ATTR_MTIME_SET)
  1274. return true;
  1275. /* Or if kernel i_mtime is the official one */
  1276. if (trust_local_mtime)
  1277. return true;
  1278. /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
  1279. if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
  1280. return false;
  1281. /* In all other cases update */
  1282. return true;
  1283. }
  1284. static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
  1285. bool trust_local_cmtime)
  1286. {
  1287. unsigned ivalid = iattr->ia_valid;
  1288. if (ivalid & ATTR_MODE)
  1289. arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
  1290. if (ivalid & ATTR_UID)
  1291. arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
  1292. if (ivalid & ATTR_GID)
  1293. arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
  1294. if (ivalid & ATTR_SIZE)
  1295. arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
  1296. if (ivalid & ATTR_ATIME) {
  1297. arg->valid |= FATTR_ATIME;
  1298. arg->atime = iattr->ia_atime.tv_sec;
  1299. arg->atimensec = iattr->ia_atime.tv_nsec;
  1300. if (!(ivalid & ATTR_ATIME_SET))
  1301. arg->valid |= FATTR_ATIME_NOW;
  1302. }
  1303. if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
  1304. arg->valid |= FATTR_MTIME;
  1305. arg->mtime = iattr->ia_mtime.tv_sec;
  1306. arg->mtimensec = iattr->ia_mtime.tv_nsec;
  1307. if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
  1308. arg->valid |= FATTR_MTIME_NOW;
  1309. }
  1310. if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
  1311. arg->valid |= FATTR_CTIME;
  1312. arg->ctime = iattr->ia_ctime.tv_sec;
  1313. arg->ctimensec = iattr->ia_ctime.tv_nsec;
  1314. }
  1315. }
  1316. /*
  1317. * Prevent concurrent writepages on inode
  1318. *
  1319. * This is done by adding a negative bias to the inode write counter
  1320. * and waiting for all pending writes to finish.
  1321. */
  1322. void fuse_set_nowrite(struct inode *inode)
  1323. {
  1324. struct fuse_conn *fc = get_fuse_conn(inode);
  1325. struct fuse_inode *fi = get_fuse_inode(inode);
  1326. BUG_ON(!inode_is_locked(inode));
  1327. spin_lock(&fc->lock);
  1328. BUG_ON(fi->writectr < 0);
  1329. fi->writectr += FUSE_NOWRITE;
  1330. spin_unlock(&fc->lock);
  1331. wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
  1332. }
  1333. /*
  1334. * Allow writepages on inode
  1335. *
  1336. * Remove the bias from the writecounter and send any queued
  1337. * writepages.
  1338. */
  1339. static void __fuse_release_nowrite(struct inode *inode)
  1340. {
  1341. struct fuse_inode *fi = get_fuse_inode(inode);
  1342. BUG_ON(fi->writectr != FUSE_NOWRITE);
  1343. fi->writectr = 0;
  1344. fuse_flush_writepages(inode);
  1345. }
  1346. void fuse_release_nowrite(struct inode *inode)
  1347. {
  1348. struct fuse_conn *fc = get_fuse_conn(inode);
  1349. spin_lock(&fc->lock);
  1350. __fuse_release_nowrite(inode);
  1351. spin_unlock(&fc->lock);
  1352. }
  1353. static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
  1354. struct inode *inode,
  1355. struct fuse_setattr_in *inarg_p,
  1356. struct fuse_attr_out *outarg_p)
  1357. {
  1358. args->in.h.opcode = FUSE_SETATTR;
  1359. args->in.h.nodeid = get_node_id(inode);
  1360. args->in.numargs = 1;
  1361. args->in.args[0].size = sizeof(*inarg_p);
  1362. args->in.args[0].value = inarg_p;
  1363. args->out.numargs = 1;
  1364. args->out.args[0].size = sizeof(*outarg_p);
  1365. args->out.args[0].value = outarg_p;
  1366. }
  1367. /*
  1368. * Flush inode->i_mtime to the server
  1369. */
  1370. int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
  1371. {
  1372. struct fuse_conn *fc = get_fuse_conn(inode);
  1373. FUSE_ARGS(args);
  1374. struct fuse_setattr_in inarg;
  1375. struct fuse_attr_out outarg;
  1376. memset(&inarg, 0, sizeof(inarg));
  1377. memset(&outarg, 0, sizeof(outarg));
  1378. inarg.valid = FATTR_MTIME;
  1379. inarg.mtime = inode->i_mtime.tv_sec;
  1380. inarg.mtimensec = inode->i_mtime.tv_nsec;
  1381. if (fc->minor >= 23) {
  1382. inarg.valid |= FATTR_CTIME;
  1383. inarg.ctime = inode->i_ctime.tv_sec;
  1384. inarg.ctimensec = inode->i_ctime.tv_nsec;
  1385. }
  1386. if (ff) {
  1387. inarg.valid |= FATTR_FH;
  1388. inarg.fh = ff->fh;
  1389. }
  1390. fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
  1391. return fuse_simple_request(fc, &args);
  1392. }
  1393. /*
  1394. * Set attributes, and at the same time refresh them.
  1395. *
  1396. * Truncation is slightly complicated, because the 'truncate' request
  1397. * may fail, in which case we don't want to touch the mapping.
  1398. * vmtruncate() doesn't allow for this case, so do the rlimit checking
  1399. * and the actual truncation by hand.
  1400. */
  1401. int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
  1402. struct file *file)
  1403. {
  1404. struct inode *inode = d_inode(dentry);
  1405. struct fuse_conn *fc = get_fuse_conn(inode);
  1406. struct fuse_inode *fi = get_fuse_inode(inode);
  1407. FUSE_ARGS(args);
  1408. struct fuse_setattr_in inarg;
  1409. struct fuse_attr_out outarg;
  1410. bool is_truncate = false;
  1411. bool is_wb = fc->writeback_cache;
  1412. loff_t oldsize;
  1413. int err;
  1414. bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
  1415. if (!fc->default_permissions)
  1416. attr->ia_valid |= ATTR_FORCE;
  1417. err = setattr_prepare(dentry, attr);
  1418. if (err)
  1419. return err;
  1420. if (attr->ia_valid & ATTR_OPEN) {
  1421. /* This is coming from open(..., ... | O_TRUNC); */
  1422. WARN_ON(!(attr->ia_valid & ATTR_SIZE));
  1423. WARN_ON(attr->ia_size != 0);
  1424. if (fc->atomic_o_trunc) {
  1425. /*
  1426. * No need to send request to userspace, since actual
  1427. * truncation has already been done by OPEN. But still
  1428. * need to truncate page cache.
  1429. */
  1430. i_size_write(inode, 0);
  1431. truncate_pagecache(inode, 0);
  1432. return 0;
  1433. }
  1434. file = NULL;
  1435. }
  1436. if (attr->ia_valid & ATTR_SIZE)
  1437. is_truncate = true;
  1438. if (is_truncate) {
  1439. fuse_set_nowrite(inode);
  1440. set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  1441. if (trust_local_cmtime && attr->ia_size != inode->i_size)
  1442. attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
  1443. }
  1444. memset(&inarg, 0, sizeof(inarg));
  1445. memset(&outarg, 0, sizeof(outarg));
  1446. iattr_to_fattr(attr, &inarg, trust_local_cmtime);
  1447. if (file) {
  1448. struct fuse_file *ff = file->private_data;
  1449. inarg.valid |= FATTR_FH;
  1450. inarg.fh = ff->fh;
  1451. }
  1452. if (attr->ia_valid & ATTR_SIZE) {
  1453. /* For mandatory locking in truncate */
  1454. inarg.valid |= FATTR_LOCKOWNER;
  1455. inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
  1456. }
  1457. fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
  1458. err = fuse_simple_request(fc, &args);
  1459. if (err) {
  1460. if (err == -EINTR)
  1461. fuse_invalidate_attr(inode);
  1462. goto error;
  1463. }
  1464. if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
  1465. make_bad_inode(inode);
  1466. err = -EIO;
  1467. goto error;
  1468. }
  1469. spin_lock(&fc->lock);
  1470. /* the kernel maintains i_mtime locally */
  1471. if (trust_local_cmtime) {
  1472. if (attr->ia_valid & ATTR_MTIME)
  1473. inode->i_mtime = attr->ia_mtime;
  1474. if (attr->ia_valid & ATTR_CTIME)
  1475. inode->i_ctime = attr->ia_ctime;
  1476. /* FIXME: clear I_DIRTY_SYNC? */
  1477. }
  1478. fuse_change_attributes_common(inode, &outarg.attr,
  1479. attr_timeout(&outarg));
  1480. oldsize = inode->i_size;
  1481. /* see the comment in fuse_change_attributes() */
  1482. if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
  1483. i_size_write(inode, outarg.attr.size);
  1484. if (is_truncate) {
  1485. /* NOTE: this may release/reacquire fc->lock */
  1486. __fuse_release_nowrite(inode);
  1487. }
  1488. spin_unlock(&fc->lock);
  1489. /*
  1490. * Only call invalidate_inode_pages2() after removing
  1491. * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
  1492. */
  1493. if ((is_truncate || !is_wb) &&
  1494. S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
  1495. truncate_pagecache(inode, outarg.attr.size);
  1496. invalidate_inode_pages2(inode->i_mapping);
  1497. }
  1498. clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  1499. return 0;
  1500. error:
  1501. if (is_truncate)
  1502. fuse_release_nowrite(inode);
  1503. clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
  1504. return err;
  1505. }
  1506. static int fuse_setattr(struct dentry *entry, struct iattr *attr)
  1507. {
  1508. struct inode *inode = d_inode(entry);
  1509. struct fuse_conn *fc = get_fuse_conn(inode);
  1510. struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
  1511. int ret;
  1512. if (!fuse_allow_current_process(get_fuse_conn(inode)))
  1513. return -EACCES;
  1514. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
  1515. attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
  1516. ATTR_MODE);
  1517. /*
  1518. * The only sane way to reliably kill suid/sgid is to do it in
  1519. * the userspace filesystem
  1520. *
  1521. * This should be done on write(), truncate() and chown().
  1522. */
  1523. if (!fc->handle_killpriv) {
  1524. /*
  1525. * ia_mode calculation may have used stale i_mode.
  1526. * Refresh and recalculate.
  1527. */
  1528. ret = fuse_do_getattr(inode, NULL, file);
  1529. if (ret)
  1530. return ret;
  1531. attr->ia_mode = inode->i_mode;
  1532. if (inode->i_mode & S_ISUID) {
  1533. attr->ia_valid |= ATTR_MODE;
  1534. attr->ia_mode &= ~S_ISUID;
  1535. }
  1536. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  1537. attr->ia_valid |= ATTR_MODE;
  1538. attr->ia_mode &= ~S_ISGID;
  1539. }
  1540. }
  1541. }
  1542. if (!attr->ia_valid)
  1543. return 0;
  1544. ret = fuse_do_setattr(entry, attr, file);
  1545. if (!ret) {
  1546. /*
  1547. * If filesystem supports acls it may have updated acl xattrs in
  1548. * the filesystem, so forget cached acls for the inode.
  1549. */
  1550. if (fc->posix_acl)
  1551. forget_all_cached_acls(inode);
  1552. /* Directory mode changed, may need to revalidate access */
  1553. if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
  1554. fuse_invalidate_entry_cache(entry);
  1555. }
  1556. return ret;
  1557. }
  1558. static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
  1559. struct kstat *stat)
  1560. {
  1561. struct inode *inode = d_inode(entry);
  1562. struct fuse_conn *fc = get_fuse_conn(inode);
  1563. if (!fuse_allow_current_process(fc))
  1564. return -EACCES;
  1565. return fuse_update_attributes(inode, stat, NULL, NULL);
  1566. }
  1567. static const struct inode_operations fuse_dir_inode_operations = {
  1568. .lookup = fuse_lookup,
  1569. .mkdir = fuse_mkdir,
  1570. .symlink = fuse_symlink,
  1571. .unlink = fuse_unlink,
  1572. .rmdir = fuse_rmdir,
  1573. .rename = fuse_rename2,
  1574. .link = fuse_link,
  1575. .setattr = fuse_setattr,
  1576. .create = fuse_create,
  1577. .atomic_open = fuse_atomic_open,
  1578. .mknod = fuse_mknod,
  1579. .permission = fuse_permission,
  1580. .getattr = fuse_getattr,
  1581. .listxattr = fuse_listxattr,
  1582. .get_acl = fuse_get_acl,
  1583. .set_acl = fuse_set_acl,
  1584. };
  1585. static const struct file_operations fuse_dir_operations = {
  1586. .llseek = generic_file_llseek,
  1587. .read = generic_read_dir,
  1588. .iterate_shared = fuse_readdir,
  1589. .open = fuse_dir_open,
  1590. .release = fuse_dir_release,
  1591. .fsync = fuse_dir_fsync,
  1592. .unlocked_ioctl = fuse_dir_ioctl,
  1593. .compat_ioctl = fuse_dir_compat_ioctl,
  1594. };
  1595. static const struct inode_operations fuse_common_inode_operations = {
  1596. .setattr = fuse_setattr,
  1597. .permission = fuse_permission,
  1598. .getattr = fuse_getattr,
  1599. .listxattr = fuse_listxattr,
  1600. .get_acl = fuse_get_acl,
  1601. .set_acl = fuse_set_acl,
  1602. };
  1603. static const struct inode_operations fuse_symlink_inode_operations = {
  1604. .setattr = fuse_setattr,
  1605. .get_link = fuse_get_link,
  1606. .readlink = generic_readlink,
  1607. .getattr = fuse_getattr,
  1608. .listxattr = fuse_listxattr,
  1609. };
  1610. void fuse_init_common(struct inode *inode)
  1611. {
  1612. inode->i_op = &fuse_common_inode_operations;
  1613. }
  1614. void fuse_init_dir(struct inode *inode)
  1615. {
  1616. inode->i_op = &fuse_dir_inode_operations;
  1617. inode->i_fop = &fuse_dir_operations;
  1618. }
  1619. void fuse_init_symlink(struct inode *inode)
  1620. {
  1621. inode->i_op = &fuse_symlink_inode_operations;
  1622. }