dir.c 40 KB

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