namei.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  164. if (fscache_object_is_live(&xobject->fscache)) {
  165. pr_err("\n");
  166. pr_err("Error: Unexpected object collision\n");
  167. cachefiles_printk_object(object, xobject);
  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,
  212. (enum fscache_obj_ref_trace)cachefiles_obj_put_wait_retry);
  213. goto try_again;
  214. requeue:
  215. cache->cache.ops->put_object(&xobject->fscache,
  216. (enum fscache_obj_ref_trace)cachefiles_obj_put_wait_timeo);
  217. _leave(" = -ETIMEDOUT");
  218. return -ETIMEDOUT;
  219. }
  220. /*
  221. * Mark an object as being inactive.
  222. */
  223. void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
  224. struct cachefiles_object *object,
  225. blkcnt_t i_blocks)
  226. {
  227. struct dentry *dentry = object->dentry;
  228. struct inode *inode = d_backing_inode(dentry);
  229. trace_cachefiles_mark_inactive(object, dentry, inode);
  230. write_lock(&cache->active_lock);
  231. rb_erase(&object->active_node, &cache->active_nodes);
  232. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  233. write_unlock(&cache->active_lock);
  234. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  235. /* This object can now be culled, so we need to let the daemon know
  236. * that there is something it can remove if it needs to.
  237. */
  238. atomic_long_add(i_blocks, &cache->b_released);
  239. if (atomic_inc_return(&cache->f_released))
  240. cachefiles_state_changed(cache);
  241. }
  242. /*
  243. * delete an object representation from the cache
  244. * - file backed objects are unlinked
  245. * - directory backed objects are stuffed into the graveyard for userspace to
  246. * delete
  247. * - unlocks the directory mutex
  248. */
  249. static int cachefiles_bury_object(struct cachefiles_cache *cache,
  250. struct cachefiles_object *object,
  251. struct dentry *dir,
  252. struct dentry *rep,
  253. bool preemptive,
  254. enum fscache_why_object_killed why)
  255. {
  256. struct dentry *grave, *trap;
  257. struct path path, path_to_graveyard;
  258. char nbuffer[8 + 8 + 1];
  259. int ret;
  260. _enter(",'%pd','%pd'", dir, rep);
  261. _debug("remove %p from %p", rep, dir);
  262. /* non-directories can just be unlinked */
  263. if (!d_is_dir(rep)) {
  264. _debug("unlink stale object");
  265. path.mnt = cache->mnt;
  266. path.dentry = dir;
  267. ret = security_path_unlink(&path, rep);
  268. if (ret < 0) {
  269. cachefiles_io_error(cache, "Unlink security error");
  270. } else {
  271. trace_cachefiles_unlink(object, rep, why);
  272. ret = vfs_unlink(d_inode(dir), rep, NULL);
  273. if (preemptive)
  274. cachefiles_mark_object_buried(cache, rep, why);
  275. }
  276. inode_unlock(d_inode(dir));
  277. if (ret == -EIO)
  278. cachefiles_io_error(cache, "Unlink failed");
  279. _leave(" = %d", ret);
  280. return ret;
  281. }
  282. /* directories have to be moved to the graveyard */
  283. _debug("move stale object to graveyard");
  284. inode_unlock(d_inode(dir));
  285. try_again:
  286. /* first step is to make up a grave dentry in the graveyard */
  287. sprintf(nbuffer, "%08x%08x",
  288. (uint32_t) get_seconds(),
  289. (uint32_t) atomic_inc_return(&cache->gravecounter));
  290. /* do the multiway lock magic */
  291. trap = lock_rename(cache->graveyard, dir);
  292. /* do some checks before getting the grave dentry */
  293. if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) {
  294. /* the entry was probably culled when we dropped the parent dir
  295. * lock */
  296. unlock_rename(cache->graveyard, dir);
  297. _leave(" = 0 [culled?]");
  298. return 0;
  299. }
  300. if (!d_can_lookup(cache->graveyard)) {
  301. unlock_rename(cache->graveyard, dir);
  302. cachefiles_io_error(cache, "Graveyard no longer a directory");
  303. return -EIO;
  304. }
  305. if (trap == rep) {
  306. unlock_rename(cache->graveyard, dir);
  307. cachefiles_io_error(cache, "May not make directory loop");
  308. return -EIO;
  309. }
  310. if (d_mountpoint(rep)) {
  311. unlock_rename(cache->graveyard, dir);
  312. cachefiles_io_error(cache, "Mountpoint in cache");
  313. return -EIO;
  314. }
  315. grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
  316. if (IS_ERR(grave)) {
  317. unlock_rename(cache->graveyard, dir);
  318. if (PTR_ERR(grave) == -ENOMEM) {
  319. _leave(" = -ENOMEM");
  320. return -ENOMEM;
  321. }
  322. cachefiles_io_error(cache, "Lookup error %ld",
  323. PTR_ERR(grave));
  324. return -EIO;
  325. }
  326. if (d_is_positive(grave)) {
  327. unlock_rename(cache->graveyard, dir);
  328. dput(grave);
  329. grave = NULL;
  330. cond_resched();
  331. goto try_again;
  332. }
  333. if (d_mountpoint(grave)) {
  334. unlock_rename(cache->graveyard, dir);
  335. dput(grave);
  336. cachefiles_io_error(cache, "Mountpoint in graveyard");
  337. return -EIO;
  338. }
  339. /* target should not be an ancestor of source */
  340. if (trap == grave) {
  341. unlock_rename(cache->graveyard, dir);
  342. dput(grave);
  343. cachefiles_io_error(cache, "May not make directory loop");
  344. return -EIO;
  345. }
  346. /* attempt the rename */
  347. path.mnt = cache->mnt;
  348. path.dentry = dir;
  349. path_to_graveyard.mnt = cache->mnt;
  350. path_to_graveyard.dentry = cache->graveyard;
  351. ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0);
  352. if (ret < 0) {
  353. cachefiles_io_error(cache, "Rename security error %d", ret);
  354. } else {
  355. trace_cachefiles_rename(object, rep, grave, why);
  356. ret = vfs_rename(d_inode(dir), rep,
  357. d_inode(cache->graveyard), grave, NULL, 0);
  358. if (ret != 0 && ret != -ENOMEM)
  359. cachefiles_io_error(cache,
  360. "Rename failed with error %d", ret);
  361. if (preemptive)
  362. cachefiles_mark_object_buried(cache, rep, why);
  363. }
  364. unlock_rename(cache->graveyard, dir);
  365. dput(grave);
  366. _leave(" = 0");
  367. return 0;
  368. }
  369. /*
  370. * delete an object representation from the cache
  371. */
  372. int cachefiles_delete_object(struct cachefiles_cache *cache,
  373. struct cachefiles_object *object)
  374. {
  375. struct dentry *dir;
  376. int ret;
  377. _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
  378. ASSERT(object->dentry);
  379. ASSERT(d_backing_inode(object->dentry));
  380. ASSERT(object->dentry->d_parent);
  381. dir = dget_parent(object->dentry);
  382. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  383. if (test_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->fscache.flags)) {
  384. /* object allocation for the same key preemptively deleted this
  385. * object's file so that it could create its own file */
  386. _debug("object preemptively buried");
  387. inode_unlock(d_inode(dir));
  388. ret = 0;
  389. } else {
  390. /* we need to check that our parent is _still_ our parent - it
  391. * may have been renamed */
  392. if (dir == object->dentry->d_parent) {
  393. ret = cachefiles_bury_object(cache, object, dir,
  394. object->dentry, false,
  395. FSCACHE_OBJECT_WAS_RETIRED);
  396. } else {
  397. /* it got moved, presumably by cachefilesd culling it,
  398. * so it's no longer in the key path and we can ignore
  399. * it */
  400. inode_unlock(d_inode(dir));
  401. ret = 0;
  402. }
  403. }
  404. dput(dir);
  405. _leave(" = %d", ret);
  406. return ret;
  407. }
  408. /*
  409. * walk from the parent object to the child object through the backing
  410. * filesystem, creating directories as we go
  411. */
  412. int cachefiles_walk_to_object(struct cachefiles_object *parent,
  413. struct cachefiles_object *object,
  414. const char *key,
  415. struct cachefiles_xattr *auxdata)
  416. {
  417. struct cachefiles_cache *cache;
  418. struct dentry *dir, *next = NULL;
  419. struct inode *inode;
  420. struct path path;
  421. unsigned long start;
  422. const char *name;
  423. int ret, nlen;
  424. _enter("OBJ%x{%p},OBJ%x,%s,",
  425. parent->fscache.debug_id, parent->dentry,
  426. object->fscache.debug_id, key);
  427. cache = container_of(parent->fscache.cache,
  428. struct cachefiles_cache, cache);
  429. path.mnt = cache->mnt;
  430. ASSERT(parent->dentry);
  431. ASSERT(d_backing_inode(parent->dentry));
  432. if (!(d_is_dir(parent->dentry))) {
  433. // TODO: convert file to dir
  434. _leave("looking up in none directory");
  435. return -ENOBUFS;
  436. }
  437. dir = dget(parent->dentry);
  438. advance:
  439. /* attempt to transit the first directory component */
  440. name = key;
  441. nlen = strlen(key);
  442. /* key ends in a double NUL */
  443. key = key + nlen + 1;
  444. if (!*key)
  445. key = NULL;
  446. lookup_again:
  447. /* search the current directory for the element name */
  448. _debug("lookup '%s'", name);
  449. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  450. start = jiffies;
  451. next = lookup_one_len(name, dir, nlen);
  452. cachefiles_hist(cachefiles_lookup_histogram, start);
  453. if (IS_ERR(next)) {
  454. trace_cachefiles_lookup(object, next, NULL);
  455. goto lookup_error;
  456. }
  457. inode = d_backing_inode(next);
  458. trace_cachefiles_lookup(object, next, inode);
  459. _debug("next -> %p %s", next, inode ? "positive" : "negative");
  460. if (!key)
  461. object->new = !inode;
  462. /* if this element of the path doesn't exist, then the lookup phase
  463. * failed, and we can release any readers in the certain knowledge that
  464. * there's nothing for them to actually read */
  465. if (d_is_negative(next))
  466. fscache_object_lookup_negative(&object->fscache);
  467. /* we need to create the object if it's negative */
  468. if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
  469. /* index objects and intervening tree levels must be subdirs */
  470. if (d_is_negative(next)) {
  471. ret = cachefiles_has_space(cache, 1, 0);
  472. if (ret < 0)
  473. goto no_space_error;
  474. path.dentry = dir;
  475. ret = security_path_mkdir(&path, next, 0);
  476. if (ret < 0)
  477. goto create_error;
  478. start = jiffies;
  479. ret = vfs_mkdir(d_inode(dir), next, 0);
  480. cachefiles_hist(cachefiles_mkdir_histogram, start);
  481. if (!key)
  482. trace_cachefiles_mkdir(object, next, ret);
  483. if (ret < 0)
  484. goto create_error;
  485. if (unlikely(d_unhashed(next))) {
  486. dput(next);
  487. inode_unlock(d_inode(dir));
  488. goto lookup_again;
  489. }
  490. ASSERT(d_backing_inode(next));
  491. _debug("mkdir -> %p{%p{ino=%lu}}",
  492. next, d_backing_inode(next), d_backing_inode(next)->i_ino);
  493. } else if (!d_can_lookup(next)) {
  494. pr_err("inode %lu is not a directory\n",
  495. d_backing_inode(next)->i_ino);
  496. ret = -ENOBUFS;
  497. goto error;
  498. }
  499. } else {
  500. /* non-index objects start out life as files */
  501. if (d_is_negative(next)) {
  502. ret = cachefiles_has_space(cache, 1, 0);
  503. if (ret < 0)
  504. goto no_space_error;
  505. path.dentry = dir;
  506. ret = security_path_mknod(&path, next, S_IFREG, 0);
  507. if (ret < 0)
  508. goto create_error;
  509. start = jiffies;
  510. ret = vfs_create(d_inode(dir), next, S_IFREG, true);
  511. cachefiles_hist(cachefiles_create_histogram, start);
  512. trace_cachefiles_create(object, next, ret);
  513. if (ret < 0)
  514. goto create_error;
  515. ASSERT(d_backing_inode(next));
  516. _debug("create -> %p{%p{ino=%lu}}",
  517. next, d_backing_inode(next), d_backing_inode(next)->i_ino);
  518. } else if (!d_can_lookup(next) &&
  519. !d_is_reg(next)
  520. ) {
  521. pr_err("inode %lu is not a file or directory\n",
  522. d_backing_inode(next)->i_ino);
  523. ret = -ENOBUFS;
  524. goto error;
  525. }
  526. }
  527. /* process the next component */
  528. if (key) {
  529. _debug("advance");
  530. inode_unlock(d_inode(dir));
  531. dput(dir);
  532. dir = next;
  533. next = NULL;
  534. goto advance;
  535. }
  536. /* we've found the object we were looking for */
  537. object->dentry = next;
  538. /* if we've found that the terminal object exists, then we need to
  539. * check its attributes and delete it if it's out of date */
  540. if (!object->new) {
  541. _debug("validate '%pd'", next);
  542. ret = cachefiles_check_object_xattr(object, auxdata);
  543. if (ret == -ESTALE) {
  544. /* delete the object (the deleter drops the directory
  545. * mutex) */
  546. object->dentry = NULL;
  547. ret = cachefiles_bury_object(cache, object, dir, next,
  548. true,
  549. FSCACHE_OBJECT_IS_STALE);
  550. dput(next);
  551. next = NULL;
  552. if (ret < 0)
  553. goto delete_error;
  554. _debug("redo lookup");
  555. fscache_object_retrying_stale(&object->fscache);
  556. goto lookup_again;
  557. }
  558. }
  559. /* note that we're now using this object */
  560. ret = cachefiles_mark_object_active(cache, object);
  561. inode_unlock(d_inode(dir));
  562. dput(dir);
  563. dir = NULL;
  564. if (ret == -ETIMEDOUT)
  565. goto mark_active_timed_out;
  566. _debug("=== OBTAINED_OBJECT ===");
  567. if (object->new) {
  568. /* attach data to a newly constructed terminal object */
  569. ret = cachefiles_set_object_xattr(object, auxdata);
  570. if (ret < 0)
  571. goto check_error;
  572. } else {
  573. /* always update the atime on an object we've just looked up
  574. * (this is used to keep track of culling, and atimes are only
  575. * updated by read, write and readdir but not lookup or
  576. * open) */
  577. path.dentry = next;
  578. touch_atime(&path);
  579. }
  580. /* open a file interface onto a data file */
  581. if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
  582. if (d_is_reg(object->dentry)) {
  583. const struct address_space_operations *aops;
  584. ret = -EPERM;
  585. aops = d_backing_inode(object->dentry)->i_mapping->a_ops;
  586. if (!aops->bmap)
  587. goto check_error;
  588. if (object->dentry->d_sb->s_blocksize > PAGE_SIZE)
  589. goto check_error;
  590. object->backer = object->dentry;
  591. } else {
  592. BUG(); // TODO: open file in data-class subdir
  593. }
  594. }
  595. object->new = 0;
  596. fscache_obtained_object(&object->fscache);
  597. _leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
  598. return 0;
  599. no_space_error:
  600. fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
  601. create_error:
  602. _debug("create error %d", ret);
  603. if (ret == -EIO)
  604. cachefiles_io_error(cache, "Create/mkdir failed");
  605. goto error;
  606. mark_active_timed_out:
  607. _debug("mark active timed out");
  608. goto release_dentry;
  609. check_error:
  610. _debug("check error %d", ret);
  611. cachefiles_mark_object_inactive(
  612. cache, object, d_backing_inode(object->dentry)->i_blocks);
  613. release_dentry:
  614. dput(object->dentry);
  615. object->dentry = NULL;
  616. goto error_out;
  617. delete_error:
  618. _debug("delete error %d", ret);
  619. goto error_out2;
  620. lookup_error:
  621. _debug("lookup error %ld", PTR_ERR(next));
  622. ret = PTR_ERR(next);
  623. if (ret == -EIO)
  624. cachefiles_io_error(cache, "Lookup failed");
  625. next = NULL;
  626. error:
  627. inode_unlock(d_inode(dir));
  628. dput(next);
  629. error_out2:
  630. dput(dir);
  631. error_out:
  632. _leave(" = error %d", -ret);
  633. return ret;
  634. }
  635. /*
  636. * get a subdirectory
  637. */
  638. struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  639. struct dentry *dir,
  640. const char *dirname)
  641. {
  642. struct dentry *subdir;
  643. unsigned long start;
  644. struct path path;
  645. int ret;
  646. _enter(",,%s", dirname);
  647. /* search the current directory for the element name */
  648. inode_lock(d_inode(dir));
  649. retry:
  650. start = jiffies;
  651. subdir = lookup_one_len(dirname, dir, strlen(dirname));
  652. cachefiles_hist(cachefiles_lookup_histogram, start);
  653. if (IS_ERR(subdir)) {
  654. if (PTR_ERR(subdir) == -ENOMEM)
  655. goto nomem_d_alloc;
  656. goto lookup_error;
  657. }
  658. _debug("subdir -> %p %s",
  659. subdir, d_backing_inode(subdir) ? "positive" : "negative");
  660. /* we need to create the subdir if it doesn't exist yet */
  661. if (d_is_negative(subdir)) {
  662. ret = cachefiles_has_space(cache, 1, 0);
  663. if (ret < 0)
  664. goto mkdir_error;
  665. _debug("attempt mkdir");
  666. path.mnt = cache->mnt;
  667. path.dentry = dir;
  668. ret = security_path_mkdir(&path, subdir, 0700);
  669. if (ret < 0)
  670. goto mkdir_error;
  671. ret = vfs_mkdir(d_inode(dir), subdir, 0700);
  672. if (ret < 0)
  673. goto mkdir_error;
  674. if (unlikely(d_unhashed(subdir))) {
  675. dput(subdir);
  676. goto retry;
  677. }
  678. ASSERT(d_backing_inode(subdir));
  679. _debug("mkdir -> %p{%p{ino=%lu}}",
  680. subdir,
  681. d_backing_inode(subdir),
  682. d_backing_inode(subdir)->i_ino);
  683. }
  684. inode_unlock(d_inode(dir));
  685. /* we need to make sure the subdir is a directory */
  686. ASSERT(d_backing_inode(subdir));
  687. if (!d_can_lookup(subdir)) {
  688. pr_err("%s is not a directory\n", dirname);
  689. ret = -EIO;
  690. goto check_error;
  691. }
  692. ret = -EPERM;
  693. if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
  694. !d_backing_inode(subdir)->i_op->lookup ||
  695. !d_backing_inode(subdir)->i_op->mkdir ||
  696. !d_backing_inode(subdir)->i_op->create ||
  697. !d_backing_inode(subdir)->i_op->rename ||
  698. !d_backing_inode(subdir)->i_op->rmdir ||
  699. !d_backing_inode(subdir)->i_op->unlink)
  700. goto check_error;
  701. _leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
  702. return subdir;
  703. check_error:
  704. dput(subdir);
  705. _leave(" = %d [check]", ret);
  706. return ERR_PTR(ret);
  707. mkdir_error:
  708. inode_unlock(d_inode(dir));
  709. dput(subdir);
  710. pr_err("mkdir %s failed with error %d\n", dirname, ret);
  711. return ERR_PTR(ret);
  712. lookup_error:
  713. inode_unlock(d_inode(dir));
  714. ret = PTR_ERR(subdir);
  715. pr_err("Lookup %s failed with error %d\n", dirname, ret);
  716. return ERR_PTR(ret);
  717. nomem_d_alloc:
  718. inode_unlock(d_inode(dir));
  719. _leave(" = -ENOMEM");
  720. return ERR_PTR(-ENOMEM);
  721. }
  722. /*
  723. * find out if an object is in use or not
  724. * - if finds object and it's not in use:
  725. * - returns a pointer to the object and a reference on it
  726. * - returns with the directory locked
  727. */
  728. static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
  729. struct dentry *dir,
  730. char *filename)
  731. {
  732. struct cachefiles_object *object;
  733. struct rb_node *_n;
  734. struct dentry *victim;
  735. unsigned long start;
  736. int ret;
  737. //_enter(",%pd/,%s",
  738. // dir, filename);
  739. /* look up the victim */
  740. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  741. start = jiffies;
  742. victim = lookup_one_len(filename, dir, strlen(filename));
  743. cachefiles_hist(cachefiles_lookup_histogram, start);
  744. if (IS_ERR(victim))
  745. goto lookup_error;
  746. //_debug("victim -> %p %s",
  747. // victim, d_backing_inode(victim) ? "positive" : "negative");
  748. /* if the object is no longer there then we probably retired the object
  749. * at the netfs's request whilst the cull was in progress
  750. */
  751. if (d_is_negative(victim)) {
  752. inode_unlock(d_inode(dir));
  753. dput(victim);
  754. _leave(" = -ENOENT [absent]");
  755. return ERR_PTR(-ENOENT);
  756. }
  757. /* check to see if we're using this object */
  758. read_lock(&cache->active_lock);
  759. _n = cache->active_nodes.rb_node;
  760. while (_n) {
  761. object = rb_entry(_n, struct cachefiles_object, active_node);
  762. if (object->dentry > victim)
  763. _n = _n->rb_left;
  764. else if (object->dentry < victim)
  765. _n = _n->rb_right;
  766. else
  767. goto object_in_use;
  768. }
  769. read_unlock(&cache->active_lock);
  770. //_leave(" = %p", victim);
  771. return victim;
  772. object_in_use:
  773. read_unlock(&cache->active_lock);
  774. inode_unlock(d_inode(dir));
  775. dput(victim);
  776. //_leave(" = -EBUSY [in use]");
  777. return ERR_PTR(-EBUSY);
  778. lookup_error:
  779. inode_unlock(d_inode(dir));
  780. ret = PTR_ERR(victim);
  781. if (ret == -ENOENT) {
  782. /* file or dir now absent - probably retired by netfs */
  783. _leave(" = -ESTALE [absent]");
  784. return ERR_PTR(-ESTALE);
  785. }
  786. if (ret == -EIO) {
  787. cachefiles_io_error(cache, "Lookup failed");
  788. } else if (ret != -ENOMEM) {
  789. pr_err("Internal error: %d\n", ret);
  790. ret = -EIO;
  791. }
  792. _leave(" = %d", ret);
  793. return ERR_PTR(ret);
  794. }
  795. /*
  796. * cull an object if it's not in use
  797. * - called only by cache manager daemon
  798. */
  799. int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  800. char *filename)
  801. {
  802. struct dentry *victim;
  803. int ret;
  804. _enter(",%pd/,%s", dir, filename);
  805. victim = cachefiles_check_active(cache, dir, filename);
  806. if (IS_ERR(victim))
  807. return PTR_ERR(victim);
  808. _debug("victim -> %p %s",
  809. victim, d_backing_inode(victim) ? "positive" : "negative");
  810. /* okay... the victim is not being used so we can cull it
  811. * - start by marking it as stale
  812. */
  813. _debug("victim is cullable");
  814. ret = cachefiles_remove_object_xattr(cache, victim);
  815. if (ret < 0)
  816. goto error_unlock;
  817. /* actually remove the victim (drops the dir mutex) */
  818. _debug("bury");
  819. ret = cachefiles_bury_object(cache, NULL, dir, victim, false,
  820. FSCACHE_OBJECT_WAS_CULLED);
  821. if (ret < 0)
  822. goto error;
  823. dput(victim);
  824. _leave(" = 0");
  825. return 0;
  826. error_unlock:
  827. inode_unlock(d_inode(dir));
  828. error:
  829. dput(victim);
  830. if (ret == -ENOENT) {
  831. /* file or dir now absent - probably retired by netfs */
  832. _leave(" = -ESTALE [absent]");
  833. return -ESTALE;
  834. }
  835. if (ret != -ENOMEM) {
  836. pr_err("Internal error: %d\n", ret);
  837. ret = -EIO;
  838. }
  839. _leave(" = %d", ret);
  840. return ret;
  841. }
  842. /*
  843. * find out if an object is in use or not
  844. * - called only by cache manager daemon
  845. * - returns -EBUSY or 0 to indicate whether an object is in use or not
  846. */
  847. int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
  848. char *filename)
  849. {
  850. struct dentry *victim;
  851. //_enter(",%pd/,%s",
  852. // dir, filename);
  853. victim = cachefiles_check_active(cache, dir, filename);
  854. if (IS_ERR(victim))
  855. return PTR_ERR(victim);
  856. inode_unlock(d_inode(dir));
  857. dput(victim);
  858. //_leave(" = 0");
  859. return 0;
  860. }