namei.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /* CacheFiles path walking and related routines
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/file.h>
  14. #include <linux/fs.h>
  15. #include <linux/fsnotify.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/xattr.h>
  18. #include <linux/mount.h>
  19. #include <linux/namei.h>
  20. #include <linux/security.h>
  21. #include <linux/slab.h>
  22. #include <linux/xattr.h>
  23. #include "internal.h"
  24. #define CACHEFILES_KEYBUF_SIZE 512
  25. /*
  26. * dump debugging info about an object
  27. */
  28. static noinline
  29. void __cachefiles_printk_object(struct cachefiles_object *object,
  30. const char *prefix)
  31. {
  32. struct fscache_cookie *cookie;
  33. const u8 *k;
  34. unsigned loop;
  35. pr_err("%sobject: OBJ%x\n", prefix, object->fscache.debug_id);
  36. pr_err("%sobjstate=%s fl=%lx wbusy=%x ev=%lx[%lx]\n",
  37. prefix, object->fscache.state->name,
  38. object->fscache.flags, work_busy(&object->fscache.work),
  39. object->fscache.events, object->fscache.event_mask);
  40. pr_err("%sops=%u inp=%u exc=%u\n",
  41. prefix, object->fscache.n_ops, object->fscache.n_in_progress,
  42. object->fscache.n_exclusive);
  43. pr_err("%sparent=%p\n",
  44. prefix, object->fscache.parent);
  45. spin_lock(&object->fscache.lock);
  46. cookie = object->fscache.cookie;
  47. if (cookie) {
  48. pr_err("%scookie=%p [pr=%p nd=%p fl=%lx]\n",
  49. prefix,
  50. object->fscache.cookie,
  51. object->fscache.cookie->parent,
  52. object->fscache.cookie->netfs_data,
  53. object->fscache.cookie->flags);
  54. pr_err("%skey=[%u] '", prefix, cookie->key_len);
  55. k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
  56. cookie->inline_key : cookie->key;
  57. for (loop = 0; loop < cookie->key_len; loop++)
  58. pr_cont("%02x", k[loop]);
  59. pr_cont("'\n");
  60. } else {
  61. pr_err("%scookie=NULL\n", prefix);
  62. }
  63. spin_unlock(&object->fscache.lock);
  64. }
  65. /*
  66. * dump debugging info about a pair of objects
  67. */
  68. static noinline void cachefiles_printk_object(struct cachefiles_object *object,
  69. struct cachefiles_object *xobject)
  70. {
  71. if (object)
  72. __cachefiles_printk_object(object, "");
  73. if (xobject)
  74. __cachefiles_printk_object(xobject, "x");
  75. }
  76. /*
  77. * mark the owner of a dentry, if there is one, to indicate that that dentry
  78. * has been preemptively deleted
  79. * - the caller must hold the i_mutex on the dentry's parent as required to
  80. * call vfs_unlink(), vfs_rmdir() or vfs_rename()
  81. */
  82. static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
  83. struct dentry *dentry,
  84. enum fscache_why_object_killed why)
  85. {
  86. struct cachefiles_object *object;
  87. struct rb_node *p;
  88. _enter(",'%pd'", dentry);
  89. write_lock(&cache->active_lock);
  90. p = cache->active_nodes.rb_node;
  91. while (p) {
  92. object = rb_entry(p, struct cachefiles_object, active_node);
  93. if (object->dentry > dentry)
  94. p = p->rb_left;
  95. else if (object->dentry < dentry)
  96. p = p->rb_right;
  97. else
  98. goto found_dentry;
  99. }
  100. write_unlock(&cache->active_lock);
  101. trace_cachefiles_mark_buried(NULL, dentry, why);
  102. _leave(" [no owner]");
  103. return;
  104. /* found the dentry for */
  105. found_dentry:
  106. kdebug("preemptive burial: OBJ%x [%s] %p",
  107. object->fscache.debug_id,
  108. object->fscache.state->name,
  109. dentry);
  110. trace_cachefiles_mark_buried(object, dentry, why);
  111. if (fscache_object_is_live(&object->fscache)) {
  112. pr_err("\n");
  113. pr_err("Error: Can't preemptively bury live object\n");
  114. cachefiles_printk_object(object, NULL);
  115. } else {
  116. if (why != FSCACHE_OBJECT_IS_STALE)
  117. fscache_object_mark_killed(&object->fscache, why);
  118. }
  119. write_unlock(&cache->active_lock);
  120. _leave(" [owner marked]");
  121. }
  122. /*
  123. * record the fact that an object is now active
  124. */
  125. static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
  126. struct cachefiles_object *object)
  127. {
  128. struct cachefiles_object *xobject;
  129. struct rb_node **_p, *_parent = NULL;
  130. struct dentry *dentry;
  131. _enter(",%p", object);
  132. try_again:
  133. write_lock(&cache->active_lock);
  134. dentry = object->dentry;
  135. trace_cachefiles_mark_active(object, dentry);
  136. if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
  137. pr_err("Error: Object already active\n");
  138. cachefiles_printk_object(object, NULL);
  139. BUG();
  140. }
  141. _p = &cache->active_nodes.rb_node;
  142. while (*_p) {
  143. _parent = *_p;
  144. xobject = rb_entry(_parent,
  145. struct cachefiles_object, active_node);
  146. ASSERT(xobject != object);
  147. if (xobject->dentry > dentry)
  148. _p = &(*_p)->rb_left;
  149. else if (xobject->dentry < dentry)
  150. _p = &(*_p)->rb_right;
  151. else
  152. goto wait_for_old_object;
  153. }
  154. rb_link_node(&object->active_node, _parent, _p);
  155. rb_insert_color(&object->active_node, &cache->active_nodes);
  156. write_unlock(&cache->active_lock);
  157. _leave(" = 0");
  158. return 0;
  159. /* an old object from a previous incarnation is hogging the slot - we
  160. * need to wait for it to be destroyed */
  161. wait_for_old_object:
  162. trace_cachefiles_wait_active(object, dentry, xobject);
  163. if (fscache_object_is_live(&xobject->fscache)) {
  164. pr_err("\n");
  165. pr_err("Error: Unexpected object collision\n");
  166. cachefiles_printk_object(object, xobject);
  167. BUG();
  168. }
  169. atomic_inc(&xobject->usage);
  170. write_unlock(&cache->active_lock);
  171. if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
  172. wait_queue_head_t *wq;
  173. signed long timeout = 60 * HZ;
  174. wait_queue_entry_t wait;
  175. bool requeue;
  176. /* if the object we're waiting for is queued for processing,
  177. * then just put ourselves on the queue behind it */
  178. if (work_pending(&xobject->fscache.work)) {
  179. _debug("queue OBJ%x behind OBJ%x immediately",
  180. object->fscache.debug_id,
  181. xobject->fscache.debug_id);
  182. goto requeue;
  183. }
  184. /* otherwise we sleep until either the object we're waiting for
  185. * is done, or the fscache_object is congested */
  186. wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
  187. init_wait(&wait);
  188. requeue = false;
  189. do {
  190. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  191. if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
  192. break;
  193. requeue = fscache_object_sleep_till_congested(&timeout);
  194. } while (timeout > 0 && !requeue);
  195. finish_wait(wq, &wait);
  196. if (requeue &&
  197. test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
  198. _debug("queue OBJ%x behind OBJ%x after wait",
  199. object->fscache.debug_id,
  200. xobject->fscache.debug_id);
  201. goto requeue;
  202. }
  203. if (timeout <= 0) {
  204. pr_err("\n");
  205. pr_err("Error: Overlong wait for old active object to go away\n");
  206. cachefiles_printk_object(object, xobject);
  207. goto requeue;
  208. }
  209. }
  210. ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
  211. cache->cache.ops->put_object(&xobject->fscache, cachefiles_obj_put_wait_retry);
  212. goto try_again;
  213. requeue:
  214. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  215. cache->cache.ops->put_object(&xobject->fscache, cachefiles_obj_put_wait_timeo);
  216. _leave(" = -ETIMEDOUT");
  217. return -ETIMEDOUT;
  218. }
  219. /*
  220. * Mark an object as being inactive.
  221. */
  222. void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
  223. struct cachefiles_object *object,
  224. blkcnt_t i_blocks)
  225. {
  226. struct dentry *dentry = object->dentry;
  227. struct inode *inode = d_backing_inode(dentry);
  228. trace_cachefiles_mark_inactive(object, dentry, inode);
  229. write_lock(&cache->active_lock);
  230. rb_erase(&object->active_node, &cache->active_nodes);
  231. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  232. write_unlock(&cache->active_lock);
  233. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  234. /* This object can now be culled, so we need to let the daemon know
  235. * that there is something it can remove if it needs to.
  236. */
  237. atomic_long_add(i_blocks, &cache->b_released);
  238. if (atomic_inc_return(&cache->f_released))
  239. cachefiles_state_changed(cache);
  240. }
  241. /*
  242. * delete an object representation from the cache
  243. * - file backed objects are unlinked
  244. * - directory backed objects are stuffed into the graveyard for userspace to
  245. * delete
  246. * - unlocks the directory mutex
  247. */
  248. static int cachefiles_bury_object(struct cachefiles_cache *cache,
  249. struct cachefiles_object *object,
  250. struct dentry *dir,
  251. struct dentry *rep,
  252. bool preemptive,
  253. enum fscache_why_object_killed why)
  254. {
  255. struct dentry *grave, *trap;
  256. struct path path, path_to_graveyard;
  257. char nbuffer[8 + 8 + 1];
  258. int ret;
  259. _enter(",'%pd','%pd'", dir, rep);
  260. _debug("remove %p from %p", rep, dir);
  261. /* non-directories can just be unlinked */
  262. if (!d_is_dir(rep)) {
  263. _debug("unlink stale object");
  264. path.mnt = cache->mnt;
  265. path.dentry = dir;
  266. ret = security_path_unlink(&path, rep);
  267. if (ret < 0) {
  268. cachefiles_io_error(cache, "Unlink security error");
  269. } else {
  270. trace_cachefiles_unlink(object, rep, why);
  271. ret = vfs_unlink(d_inode(dir), rep, NULL);
  272. if (preemptive)
  273. cachefiles_mark_object_buried(cache, rep, why);
  274. }
  275. inode_unlock(d_inode(dir));
  276. if (ret == -EIO)
  277. cachefiles_io_error(cache, "Unlink failed");
  278. _leave(" = %d", ret);
  279. return ret;
  280. }
  281. /* directories have to be moved to the graveyard */
  282. _debug("move stale object to graveyard");
  283. inode_unlock(d_inode(dir));
  284. try_again:
  285. /* first step is to make up a grave dentry in the graveyard */
  286. sprintf(nbuffer, "%08x%08x",
  287. (uint32_t) get_seconds(),
  288. (uint32_t) atomic_inc_return(&cache->gravecounter));
  289. /* do the multiway lock magic */
  290. trap = lock_rename(cache->graveyard, dir);
  291. /* do some checks before getting the grave dentry */
  292. if (rep->d_parent != dir) {
  293. /* the entry was probably culled when we dropped the parent dir
  294. * lock */
  295. unlock_rename(cache->graveyard, dir);
  296. _leave(" = 0 [culled?]");
  297. return 0;
  298. }
  299. if (!d_can_lookup(cache->graveyard)) {
  300. unlock_rename(cache->graveyard, dir);
  301. cachefiles_io_error(cache, "Graveyard no longer a directory");
  302. return -EIO;
  303. }
  304. if (trap == rep) {
  305. unlock_rename(cache->graveyard, dir);
  306. cachefiles_io_error(cache, "May not make directory loop");
  307. return -EIO;
  308. }
  309. if (d_mountpoint(rep)) {
  310. unlock_rename(cache->graveyard, dir);
  311. cachefiles_io_error(cache, "Mountpoint in cache");
  312. return -EIO;
  313. }
  314. grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
  315. if (IS_ERR(grave)) {
  316. unlock_rename(cache->graveyard, dir);
  317. if (PTR_ERR(grave) == -ENOMEM) {
  318. _leave(" = -ENOMEM");
  319. return -ENOMEM;
  320. }
  321. cachefiles_io_error(cache, "Lookup error %ld",
  322. PTR_ERR(grave));
  323. return -EIO;
  324. }
  325. if (d_is_positive(grave)) {
  326. unlock_rename(cache->graveyard, dir);
  327. dput(grave);
  328. grave = NULL;
  329. cond_resched();
  330. goto try_again;
  331. }
  332. if (d_mountpoint(grave)) {
  333. unlock_rename(cache->graveyard, dir);
  334. dput(grave);
  335. cachefiles_io_error(cache, "Mountpoint in graveyard");
  336. return -EIO;
  337. }
  338. /* target should not be an ancestor of source */
  339. if (trap == grave) {
  340. unlock_rename(cache->graveyard, dir);
  341. dput(grave);
  342. cachefiles_io_error(cache, "May not make directory loop");
  343. return -EIO;
  344. }
  345. /* attempt the rename */
  346. path.mnt = cache->mnt;
  347. path.dentry = dir;
  348. path_to_graveyard.mnt = cache->mnt;
  349. path_to_graveyard.dentry = cache->graveyard;
  350. ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
  351. if (ret < 0) {
  352. cachefiles_io_error(cache, "Rename security error %d", ret);
  353. } else {
  354. trace_cachefiles_rename(object, rep, grave, why);
  355. ret = vfs_rename(d_inode(dir), rep,
  356. d_inode(cache->graveyard), grave, NULL, 0);
  357. if (ret != 0 && ret != -ENOMEM)
  358. cachefiles_io_error(cache,
  359. "Rename failed with error %d", ret);
  360. if (preemptive)
  361. cachefiles_mark_object_buried(cache, rep, why);
  362. }
  363. unlock_rename(cache->graveyard, dir);
  364. dput(grave);
  365. _leave(" = 0");
  366. return 0;
  367. }
  368. /*
  369. * delete an object representation from the cache
  370. */
  371. int cachefiles_delete_object(struct cachefiles_cache *cache,
  372. struct cachefiles_object *object)
  373. {
  374. struct dentry *dir;
  375. int ret;
  376. _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
  377. ASSERT(object->dentry);
  378. ASSERT(d_backing_inode(object->dentry));
  379. ASSERT(object->dentry->d_parent);
  380. dir = dget_parent(object->dentry);
  381. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  382. if (test_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->fscache.flags)) {
  383. /* object allocation for the same key preemptively deleted this
  384. * object's file so that it could create its own file */
  385. _debug("object preemptively buried");
  386. inode_unlock(d_inode(dir));
  387. ret = 0;
  388. } else {
  389. /* we need to check that our parent is _still_ our parent - it
  390. * may have been renamed */
  391. if (dir == object->dentry->d_parent) {
  392. ret = cachefiles_bury_object(cache, object, dir,
  393. object->dentry, false,
  394. FSCACHE_OBJECT_WAS_RETIRED);
  395. } else {
  396. /* it got moved, presumably by cachefilesd culling it,
  397. * so it's no longer in the key path and we can ignore
  398. * it */
  399. inode_unlock(d_inode(dir));
  400. ret = 0;
  401. }
  402. }
  403. dput(dir);
  404. _leave(" = %d", ret);
  405. return ret;
  406. }
  407. /*
  408. * walk from the parent object to the child object through the backing
  409. * filesystem, creating directories as we go
  410. */
  411. int cachefiles_walk_to_object(struct cachefiles_object *parent,
  412. struct cachefiles_object *object,
  413. const char *key,
  414. struct cachefiles_xattr *auxdata)
  415. {
  416. struct cachefiles_cache *cache;
  417. struct dentry *dir, *next = NULL;
  418. struct inode *inode;
  419. struct path path;
  420. unsigned long start;
  421. const char *name;
  422. int ret, nlen;
  423. _enter("OBJ%x{%p},OBJ%x,%s,",
  424. parent->fscache.debug_id, parent->dentry,
  425. object->fscache.debug_id, key);
  426. cache = container_of(parent->fscache.cache,
  427. struct cachefiles_cache, cache);
  428. path.mnt = cache->mnt;
  429. ASSERT(parent->dentry);
  430. ASSERT(d_backing_inode(parent->dentry));
  431. if (!(d_is_dir(parent->dentry))) {
  432. // TODO: convert file to dir
  433. _leave("looking up in none directory");
  434. return -ENOBUFS;
  435. }
  436. dir = dget(parent->dentry);
  437. advance:
  438. /* attempt to transit the first directory component */
  439. name = key;
  440. nlen = strlen(key);
  441. /* key ends in a double NUL */
  442. key = key + nlen + 1;
  443. if (!*key)
  444. key = NULL;
  445. lookup_again:
  446. /* search the current directory for the element name */
  447. _debug("lookup '%s'", name);
  448. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  449. start = jiffies;
  450. next = lookup_one_len(name, dir, nlen);
  451. cachefiles_hist(cachefiles_lookup_histogram, start);
  452. if (IS_ERR(next)) {
  453. trace_cachefiles_lookup(object, next, NULL);
  454. goto lookup_error;
  455. }
  456. inode = d_backing_inode(next);
  457. trace_cachefiles_lookup(object, next, inode);
  458. _debug("next -> %p %s", next, inode ? "positive" : "negative");
  459. if (!key)
  460. object->new = !inode;
  461. /* if this element of the path doesn't exist, then the lookup phase
  462. * failed, and we can release any readers in the certain knowledge that
  463. * there's nothing for them to actually read */
  464. if (d_is_negative(next))
  465. fscache_object_lookup_negative(&object->fscache);
  466. /* we need to create the object if it's negative */
  467. if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
  468. /* index objects and intervening tree levels must be subdirs */
  469. if (d_is_negative(next)) {
  470. ret = cachefiles_has_space(cache, 1, 0);
  471. if (ret < 0)
  472. goto no_space_error;
  473. path.dentry = dir;
  474. ret = security_path_mkdir(&path, next, 0);
  475. if (ret < 0)
  476. goto create_error;
  477. start = jiffies;
  478. ret = vfs_mkdir(d_inode(dir), next, 0);
  479. cachefiles_hist(cachefiles_mkdir_histogram, start);
  480. if (!key)
  481. trace_cachefiles_mkdir(object, next, ret);
  482. if (ret < 0)
  483. goto create_error;
  484. if (unlikely(d_unhashed(next))) {
  485. dput(next);
  486. inode_unlock(d_inode(dir));
  487. goto lookup_again;
  488. }
  489. ASSERT(d_backing_inode(next));
  490. _debug("mkdir -> %p{%p{ino=%lu}}",
  491. next, d_backing_inode(next), d_backing_inode(next)->i_ino);
  492. } else if (!d_can_lookup(next)) {
  493. pr_err("inode %lu is not a directory\n",
  494. d_backing_inode(next)->i_ino);
  495. ret = -ENOBUFS;
  496. goto error;
  497. }
  498. } else {
  499. /* non-index objects start out life as files */
  500. if (d_is_negative(next)) {
  501. ret = cachefiles_has_space(cache, 1, 0);
  502. if (ret < 0)
  503. goto no_space_error;
  504. path.dentry = dir;
  505. ret = security_path_mknod(&path, next, S_IFREG, 0);
  506. if (ret < 0)
  507. goto create_error;
  508. start = jiffies;
  509. ret = vfs_create(d_inode(dir), next, S_IFREG, true);
  510. cachefiles_hist(cachefiles_create_histogram, start);
  511. trace_cachefiles_create(object, next, ret);
  512. if (ret < 0)
  513. goto create_error;
  514. ASSERT(d_backing_inode(next));
  515. _debug("create -> %p{%p{ino=%lu}}",
  516. next, d_backing_inode(next), d_backing_inode(next)->i_ino);
  517. } else if (!d_can_lookup(next) &&
  518. !d_is_reg(next)
  519. ) {
  520. pr_err("inode %lu is not a file or directory\n",
  521. d_backing_inode(next)->i_ino);
  522. ret = -ENOBUFS;
  523. goto error;
  524. }
  525. }
  526. /* process the next component */
  527. if (key) {
  528. _debug("advance");
  529. inode_unlock(d_inode(dir));
  530. dput(dir);
  531. dir = next;
  532. next = NULL;
  533. goto advance;
  534. }
  535. /* we've found the object we were looking for */
  536. object->dentry = next;
  537. /* if we've found that the terminal object exists, then we need to
  538. * check its attributes and delete it if it's out of date */
  539. if (!object->new) {
  540. _debug("validate '%pd'", next);
  541. ret = cachefiles_check_object_xattr(object, auxdata);
  542. if (ret == -ESTALE) {
  543. /* delete the object (the deleter drops the directory
  544. * mutex) */
  545. object->dentry = NULL;
  546. ret = cachefiles_bury_object(cache, object, dir, next,
  547. true,
  548. FSCACHE_OBJECT_IS_STALE);
  549. dput(next);
  550. next = NULL;
  551. if (ret < 0)
  552. goto delete_error;
  553. _debug("redo lookup");
  554. fscache_object_retrying_stale(&object->fscache);
  555. goto lookup_again;
  556. }
  557. }
  558. /* note that we're now using this object */
  559. ret = cachefiles_mark_object_active(cache, object);
  560. inode_unlock(d_inode(dir));
  561. dput(dir);
  562. dir = NULL;
  563. if (ret == -ETIMEDOUT)
  564. goto mark_active_timed_out;
  565. _debug("=== OBTAINED_OBJECT ===");
  566. if (object->new) {
  567. /* attach data to a newly constructed terminal object */
  568. ret = cachefiles_set_object_xattr(object, auxdata);
  569. if (ret < 0)
  570. goto check_error;
  571. } else {
  572. /* always update the atime on an object we've just looked up
  573. * (this is used to keep track of culling, and atimes are only
  574. * updated by read, write and readdir but not lookup or
  575. * open) */
  576. path.dentry = next;
  577. touch_atime(&path);
  578. }
  579. /* open a file interface onto a data file */
  580. if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
  581. if (d_is_reg(object->dentry)) {
  582. const struct address_space_operations *aops;
  583. ret = -EPERM;
  584. aops = d_backing_inode(object->dentry)->i_mapping->a_ops;
  585. if (!aops->bmap)
  586. goto check_error;
  587. if (object->dentry->d_sb->s_blocksize > PAGE_SIZE)
  588. goto check_error;
  589. object->backer = object->dentry;
  590. } else {
  591. BUG(); // TODO: open file in data-class subdir
  592. }
  593. }
  594. object->new = 0;
  595. fscache_obtained_object(&object->fscache);
  596. _leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
  597. return 0;
  598. no_space_error:
  599. fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
  600. create_error:
  601. _debug("create error %d", ret);
  602. if (ret == -EIO)
  603. cachefiles_io_error(cache, "Create/mkdir failed");
  604. goto error;
  605. mark_active_timed_out:
  606. _debug("mark active timed out");
  607. goto release_dentry;
  608. check_error:
  609. _debug("check error %d", ret);
  610. cachefiles_mark_object_inactive(
  611. cache, object, d_backing_inode(object->dentry)->i_blocks);
  612. release_dentry:
  613. dput(object->dentry);
  614. object->dentry = NULL;
  615. goto error_out;
  616. delete_error:
  617. _debug("delete error %d", ret);
  618. goto error_out2;
  619. lookup_error:
  620. _debug("lookup error %ld", PTR_ERR(next));
  621. ret = PTR_ERR(next);
  622. if (ret == -EIO)
  623. cachefiles_io_error(cache, "Lookup failed");
  624. next = NULL;
  625. error:
  626. inode_unlock(d_inode(dir));
  627. dput(next);
  628. error_out2:
  629. dput(dir);
  630. error_out:
  631. _leave(" = error %d", -ret);
  632. return ret;
  633. }
  634. /*
  635. * get a subdirectory
  636. */
  637. struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  638. struct dentry *dir,
  639. const char *dirname)
  640. {
  641. struct dentry *subdir;
  642. unsigned long start;
  643. struct path path;
  644. int ret;
  645. _enter(",,%s", dirname);
  646. /* search the current directory for the element name */
  647. inode_lock(d_inode(dir));
  648. retry:
  649. start = jiffies;
  650. subdir = lookup_one_len(dirname, dir, strlen(dirname));
  651. cachefiles_hist(cachefiles_lookup_histogram, start);
  652. if (IS_ERR(subdir)) {
  653. if (PTR_ERR(subdir) == -ENOMEM)
  654. goto nomem_d_alloc;
  655. goto lookup_error;
  656. }
  657. _debug("subdir -> %p %s",
  658. subdir, d_backing_inode(subdir) ? "positive" : "negative");
  659. /* we need to create the subdir if it doesn't exist yet */
  660. if (d_is_negative(subdir)) {
  661. ret = cachefiles_has_space(cache, 1, 0);
  662. if (ret < 0)
  663. goto mkdir_error;
  664. _debug("attempt mkdir");
  665. path.mnt = cache->mnt;
  666. path.dentry = dir;
  667. ret = security_path_mkdir(&path, subdir, 0700);
  668. if (ret < 0)
  669. goto mkdir_error;
  670. ret = vfs_mkdir(d_inode(dir), subdir, 0700);
  671. if (ret < 0)
  672. goto mkdir_error;
  673. if (unlikely(d_unhashed(subdir))) {
  674. dput(subdir);
  675. goto retry;
  676. }
  677. ASSERT(d_backing_inode(subdir));
  678. _debug("mkdir -> %p{%p{ino=%lu}}",
  679. subdir,
  680. d_backing_inode(subdir),
  681. d_backing_inode(subdir)->i_ino);
  682. }
  683. inode_unlock(d_inode(dir));
  684. /* we need to make sure the subdir is a directory */
  685. ASSERT(d_backing_inode(subdir));
  686. if (!d_can_lookup(subdir)) {
  687. pr_err("%s is not a directory\n", dirname);
  688. ret = -EIO;
  689. goto check_error;
  690. }
  691. ret = -EPERM;
  692. if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
  693. !d_backing_inode(subdir)->i_op->lookup ||
  694. !d_backing_inode(subdir)->i_op->mkdir ||
  695. !d_backing_inode(subdir)->i_op->create ||
  696. !d_backing_inode(subdir)->i_op->rename ||
  697. !d_backing_inode(subdir)->i_op->rmdir ||
  698. !d_backing_inode(subdir)->i_op->unlink)
  699. goto check_error;
  700. _leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
  701. return subdir;
  702. check_error:
  703. dput(subdir);
  704. _leave(" = %d [check]", ret);
  705. return ERR_PTR(ret);
  706. mkdir_error:
  707. inode_unlock(d_inode(dir));
  708. dput(subdir);
  709. pr_err("mkdir %s failed with error %d\n", dirname, ret);
  710. return ERR_PTR(ret);
  711. lookup_error:
  712. inode_unlock(d_inode(dir));
  713. ret = PTR_ERR(subdir);
  714. pr_err("Lookup %s failed with error %d\n", dirname, ret);
  715. return ERR_PTR(ret);
  716. nomem_d_alloc:
  717. inode_unlock(d_inode(dir));
  718. _leave(" = -ENOMEM");
  719. return ERR_PTR(-ENOMEM);
  720. }
  721. /*
  722. * find out if an object is in use or not
  723. * - if finds object and it's not in use:
  724. * - returns a pointer to the object and a reference on it
  725. * - returns with the directory locked
  726. */
  727. static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
  728. struct dentry *dir,
  729. char *filename)
  730. {
  731. struct cachefiles_object *object;
  732. struct rb_node *_n;
  733. struct dentry *victim;
  734. unsigned long start;
  735. int ret;
  736. //_enter(",%pd/,%s",
  737. // dir, filename);
  738. /* look up the victim */
  739. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  740. start = jiffies;
  741. victim = lookup_one_len(filename, dir, strlen(filename));
  742. cachefiles_hist(cachefiles_lookup_histogram, start);
  743. if (IS_ERR(victim))
  744. goto lookup_error;
  745. //_debug("victim -> %p %s",
  746. // victim, d_backing_inode(victim) ? "positive" : "negative");
  747. /* if the object is no longer there then we probably retired the object
  748. * at the netfs's request whilst the cull was in progress
  749. */
  750. if (d_is_negative(victim)) {
  751. inode_unlock(d_inode(dir));
  752. dput(victim);
  753. _leave(" = -ENOENT [absent]");
  754. return ERR_PTR(-ENOENT);
  755. }
  756. /* check to see if we're using this object */
  757. read_lock(&cache->active_lock);
  758. _n = cache->active_nodes.rb_node;
  759. while (_n) {
  760. object = rb_entry(_n, struct cachefiles_object, active_node);
  761. if (object->dentry > victim)
  762. _n = _n->rb_left;
  763. else if (object->dentry < victim)
  764. _n = _n->rb_right;
  765. else
  766. goto object_in_use;
  767. }
  768. read_unlock(&cache->active_lock);
  769. //_leave(" = %p", victim);
  770. return victim;
  771. object_in_use:
  772. read_unlock(&cache->active_lock);
  773. inode_unlock(d_inode(dir));
  774. dput(victim);
  775. //_leave(" = -EBUSY [in use]");
  776. return ERR_PTR(-EBUSY);
  777. lookup_error:
  778. inode_unlock(d_inode(dir));
  779. ret = PTR_ERR(victim);
  780. if (ret == -ENOENT) {
  781. /* file or dir now absent - probably retired by netfs */
  782. _leave(" = -ESTALE [absent]");
  783. return ERR_PTR(-ESTALE);
  784. }
  785. if (ret == -EIO) {
  786. cachefiles_io_error(cache, "Lookup failed");
  787. } else if (ret != -ENOMEM) {
  788. pr_err("Internal error: %d\n", ret);
  789. ret = -EIO;
  790. }
  791. _leave(" = %d", ret);
  792. return ERR_PTR(ret);
  793. }
  794. /*
  795. * cull an object if it's not in use
  796. * - called only by cache manager daemon
  797. */
  798. int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  799. char *filename)
  800. {
  801. struct dentry *victim;
  802. int ret;
  803. _enter(",%pd/,%s", dir, filename);
  804. victim = cachefiles_check_active(cache, dir, filename);
  805. if (IS_ERR(victim))
  806. return PTR_ERR(victim);
  807. _debug("victim -> %p %s",
  808. victim, d_backing_inode(victim) ? "positive" : "negative");
  809. /* okay... the victim is not being used so we can cull it
  810. * - start by marking it as stale
  811. */
  812. _debug("victim is cullable");
  813. ret = cachefiles_remove_object_xattr(cache, victim);
  814. if (ret < 0)
  815. goto error_unlock;
  816. /* actually remove the victim (drops the dir mutex) */
  817. _debug("bury");
  818. ret = cachefiles_bury_object(cache, NULL, dir, victim, false,
  819. FSCACHE_OBJECT_WAS_CULLED);
  820. if (ret < 0)
  821. goto error;
  822. dput(victim);
  823. _leave(" = 0");
  824. return 0;
  825. error_unlock:
  826. inode_unlock(d_inode(dir));
  827. error:
  828. dput(victim);
  829. if (ret == -ENOENT) {
  830. /* file or dir now absent - probably retired by netfs */
  831. _leave(" = -ESTALE [absent]");
  832. return -ESTALE;
  833. }
  834. if (ret != -ENOMEM) {
  835. pr_err("Internal error: %d\n", ret);
  836. ret = -EIO;
  837. }
  838. _leave(" = %d", ret);
  839. return ret;
  840. }
  841. /*
  842. * find out if an object is in use or not
  843. * - called only by cache manager daemon
  844. * - returns -EBUSY or 0 to indicate whether an object is in use or not
  845. */
  846. int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
  847. char *filename)
  848. {
  849. struct dentry *victim;
  850. //_enter(",%pd/,%s",
  851. // dir, filename);
  852. victim = cachefiles_check_active(cache, dir, filename);
  853. if (IS_ERR(victim))
  854. return PTR_ERR(victim);
  855. inode_unlock(d_inode(dir));
  856. dput(victim);
  857. //_leave(" = 0");
  858. return 0;
  859. }