namei.c 25 KB

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