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 <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. u8 *keybuf)
  32. {
  33. struct fscache_cookie *cookie;
  34. unsigned keylen, 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. if (keybuf && cookie->def)
  55. keylen = cookie->def->get_key(cookie->netfs_data, keybuf,
  56. CACHEFILES_KEYBUF_SIZE);
  57. else
  58. keylen = 0;
  59. } else {
  60. pr_err("%scookie=NULL\n", prefix);
  61. keylen = 0;
  62. }
  63. spin_unlock(&object->fscache.lock);
  64. if (keylen) {
  65. pr_err("%skey=[%u] '", prefix, keylen);
  66. for (loop = 0; loop < keylen; loop++)
  67. pr_cont("%02x", keybuf[loop]);
  68. pr_cont("'\n");
  69. }
  70. }
  71. /*
  72. * dump debugging info about a pair of objects
  73. */
  74. static noinline void cachefiles_printk_object(struct cachefiles_object *object,
  75. struct cachefiles_object *xobject)
  76. {
  77. u8 *keybuf;
  78. keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO);
  79. if (object)
  80. __cachefiles_printk_object(object, "", keybuf);
  81. if (xobject)
  82. __cachefiles_printk_object(xobject, "x", keybuf);
  83. kfree(keybuf);
  84. }
  85. /*
  86. * mark the owner of a dentry, if there is one, to indicate that that dentry
  87. * has been preemptively deleted
  88. * - the caller must hold the i_mutex on the dentry's parent as required to
  89. * call vfs_unlink(), vfs_rmdir() or vfs_rename()
  90. */
  91. static void cachefiles_mark_object_buried(struct cachefiles_cache *cache,
  92. struct dentry *dentry,
  93. enum fscache_why_object_killed why)
  94. {
  95. struct cachefiles_object *object;
  96. struct rb_node *p;
  97. _enter(",'%pd'", dentry);
  98. write_lock(&cache->active_lock);
  99. p = cache->active_nodes.rb_node;
  100. while (p) {
  101. object = rb_entry(p, struct cachefiles_object, active_node);
  102. if (object->dentry > dentry)
  103. p = p->rb_left;
  104. else if (object->dentry < dentry)
  105. p = p->rb_right;
  106. else
  107. goto found_dentry;
  108. }
  109. write_unlock(&cache->active_lock);
  110. _leave(" [no owner]");
  111. return;
  112. /* found the dentry for */
  113. found_dentry:
  114. kdebug("preemptive burial: OBJ%x [%s] %p",
  115. object->fscache.debug_id,
  116. object->fscache.state->name,
  117. dentry);
  118. if (fscache_object_is_live(&object->fscache)) {
  119. pr_err("\n");
  120. pr_err("Error: Can't preemptively bury live object\n");
  121. cachefiles_printk_object(object, NULL);
  122. } else {
  123. if (why != FSCACHE_OBJECT_IS_STALE)
  124. fscache_object_mark_killed(&object->fscache, why);
  125. }
  126. write_unlock(&cache->active_lock);
  127. _leave(" [owner marked]");
  128. }
  129. /*
  130. * record the fact that an object is now active
  131. */
  132. static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
  133. struct cachefiles_object *object)
  134. {
  135. struct cachefiles_object *xobject;
  136. struct rb_node **_p, *_parent = NULL;
  137. struct dentry *dentry;
  138. _enter(",%p", object);
  139. try_again:
  140. write_lock(&cache->active_lock);
  141. if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
  142. pr_err("Error: Object already active\n");
  143. cachefiles_printk_object(object, NULL);
  144. BUG();
  145. }
  146. dentry = object->dentry;
  147. _p = &cache->active_nodes.rb_node;
  148. while (*_p) {
  149. _parent = *_p;
  150. xobject = rb_entry(_parent,
  151. struct cachefiles_object, active_node);
  152. ASSERT(xobject != object);
  153. if (xobject->dentry > dentry)
  154. _p = &(*_p)->rb_left;
  155. else if (xobject->dentry < dentry)
  156. _p = &(*_p)->rb_right;
  157. else
  158. goto wait_for_old_object;
  159. }
  160. rb_link_node(&object->active_node, _parent, _p);
  161. rb_insert_color(&object->active_node, &cache->active_nodes);
  162. write_unlock(&cache->active_lock);
  163. _leave(" = 0");
  164. return 0;
  165. /* an old object from a previous incarnation is hogging the slot - we
  166. * need to wait for it to be destroyed */
  167. wait_for_old_object:
  168. if (fscache_object_is_live(&xobject->fscache)) {
  169. pr_err("\n");
  170. pr_err("Error: Unexpected object collision\n");
  171. cachefiles_printk_object(object, xobject);
  172. BUG();
  173. }
  174. atomic_inc(&xobject->usage);
  175. write_unlock(&cache->active_lock);
  176. if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
  177. wait_queue_head_t *wq;
  178. signed long timeout = 60 * HZ;
  179. wait_queue_t wait;
  180. bool requeue;
  181. /* if the object we're waiting for is queued for processing,
  182. * then just put ourselves on the queue behind it */
  183. if (work_pending(&xobject->fscache.work)) {
  184. _debug("queue OBJ%x behind OBJ%x immediately",
  185. object->fscache.debug_id,
  186. xobject->fscache.debug_id);
  187. goto requeue;
  188. }
  189. /* otherwise we sleep until either the object we're waiting for
  190. * is done, or the fscache_object is congested */
  191. wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
  192. init_wait(&wait);
  193. requeue = false;
  194. do {
  195. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  196. if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
  197. break;
  198. requeue = fscache_object_sleep_till_congested(&timeout);
  199. } while (timeout > 0 && !requeue);
  200. finish_wait(wq, &wait);
  201. if (requeue &&
  202. test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
  203. _debug("queue OBJ%x behind OBJ%x after wait",
  204. object->fscache.debug_id,
  205. xobject->fscache.debug_id);
  206. goto requeue;
  207. }
  208. if (timeout <= 0) {
  209. pr_err("\n");
  210. pr_err("Error: Overlong wait for old active object to go away\n");
  211. cachefiles_printk_object(object, xobject);
  212. goto requeue;
  213. }
  214. }
  215. ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
  216. cache->cache.ops->put_object(&xobject->fscache);
  217. goto try_again;
  218. requeue:
  219. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  220. cache->cache.ops->put_object(&xobject->fscache);
  221. _leave(" = -ETIMEDOUT");
  222. return -ETIMEDOUT;
  223. }
  224. /*
  225. * Mark an object as being inactive.
  226. */
  227. void cachefiles_mark_object_inactive(struct cachefiles_cache *cache,
  228. struct cachefiles_object *object,
  229. blkcnt_t i_blocks)
  230. {
  231. write_lock(&cache->active_lock);
  232. rb_erase(&object->active_node, &cache->active_nodes);
  233. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  234. write_unlock(&cache->active_lock);
  235. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  236. /* This object can now be culled, so we need to let the daemon know
  237. * that there is something it can remove if it needs to.
  238. */
  239. atomic_long_add(i_blocks, &cache->b_released);
  240. if (atomic_inc_return(&cache->f_released))
  241. cachefiles_state_changed(cache);
  242. }
  243. /*
  244. * delete an object representation from the cache
  245. * - file backed objects are unlinked
  246. * - directory backed objects are stuffed into the graveyard for userspace to
  247. * delete
  248. * - unlocks the directory mutex
  249. */
  250. static int cachefiles_bury_object(struct cachefiles_cache *cache,
  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. 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. ret = vfs_rename(d_inode(dir), rep,
  355. d_inode(cache->graveyard), grave, NULL, 0);
  356. if (ret != 0 && ret != -ENOMEM)
  357. cachefiles_io_error(cache,
  358. "Rename failed with error %d", ret);
  359. if (preemptive)
  360. cachefiles_mark_object_buried(cache, rep, why);
  361. }
  362. unlock_rename(cache->graveyard, dir);
  363. dput(grave);
  364. _leave(" = 0");
  365. return 0;
  366. }
  367. /*
  368. * delete an object representation from the cache
  369. */
  370. int cachefiles_delete_object(struct cachefiles_cache *cache,
  371. struct cachefiles_object *object)
  372. {
  373. struct dentry *dir;
  374. int ret;
  375. _enter(",OBJ%x{%p}", object->fscache.debug_id, object->dentry);
  376. ASSERT(object->dentry);
  377. ASSERT(d_backing_inode(object->dentry));
  378. ASSERT(object->dentry->d_parent);
  379. dir = dget_parent(object->dentry);
  380. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  381. if (test_bit(FSCACHE_OBJECT_KILLED_BY_CACHE, &object->fscache.flags)) {
  382. /* object allocation for the same key preemptively deleted this
  383. * object's file so that it could create its own file */
  384. _debug("object preemptively buried");
  385. inode_unlock(d_inode(dir));
  386. ret = 0;
  387. } else {
  388. /* we need to check that our parent is _still_ our parent - it
  389. * may have been renamed */
  390. if (dir == object->dentry->d_parent) {
  391. ret = cachefiles_bury_object(cache, dir,
  392. object->dentry, false,
  393. FSCACHE_OBJECT_WAS_RETIRED);
  394. } else {
  395. /* it got moved, presumably by cachefilesd culling it,
  396. * so it's no longer in the key path and we can ignore
  397. * it */
  398. inode_unlock(d_inode(dir));
  399. ret = 0;
  400. }
  401. }
  402. dput(dir);
  403. _leave(" = %d", ret);
  404. return ret;
  405. }
  406. /*
  407. * walk from the parent object to the child object through the backing
  408. * filesystem, creating directories as we go
  409. */
  410. int cachefiles_walk_to_object(struct cachefiles_object *parent,
  411. struct cachefiles_object *object,
  412. const char *key,
  413. struct cachefiles_xattr *auxdata)
  414. {
  415. struct cachefiles_cache *cache;
  416. struct dentry *dir, *next = NULL;
  417. struct path path;
  418. unsigned long start;
  419. const char *name;
  420. int ret, nlen;
  421. _enter("OBJ%x{%p},OBJ%x,%s,",
  422. parent->fscache.debug_id, parent->dentry,
  423. object->fscache.debug_id, key);
  424. cache = container_of(parent->fscache.cache,
  425. struct cachefiles_cache, cache);
  426. path.mnt = cache->mnt;
  427. ASSERT(parent->dentry);
  428. ASSERT(d_backing_inode(parent->dentry));
  429. if (!(d_is_dir(parent->dentry))) {
  430. // TODO: convert file to dir
  431. _leave("looking up in none directory");
  432. return -ENOBUFS;
  433. }
  434. dir = dget(parent->dentry);
  435. advance:
  436. /* attempt to transit the first directory component */
  437. name = key;
  438. nlen = strlen(key);
  439. /* key ends in a double NUL */
  440. key = key + nlen + 1;
  441. if (!*key)
  442. key = NULL;
  443. lookup_again:
  444. /* search the current directory for the element name */
  445. _debug("lookup '%s'", name);
  446. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  447. start = jiffies;
  448. next = lookup_one_len(name, dir, nlen);
  449. cachefiles_hist(cachefiles_lookup_histogram, start);
  450. if (IS_ERR(next))
  451. goto lookup_error;
  452. _debug("next -> %p %s", next, d_backing_inode(next) ? "positive" : "negative");
  453. if (!key)
  454. object->new = !d_backing_inode(next);
  455. /* if this element of the path doesn't exist, then the lookup phase
  456. * failed, and we can release any readers in the certain knowledge that
  457. * there's nothing for them to actually read */
  458. if (d_is_negative(next))
  459. fscache_object_lookup_negative(&object->fscache);
  460. /* we need to create the object if it's negative */
  461. if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
  462. /* index objects and intervening tree levels must be subdirs */
  463. if (d_is_negative(next)) {
  464. ret = cachefiles_has_space(cache, 1, 0);
  465. if (ret < 0)
  466. goto no_space_error;
  467. path.dentry = dir;
  468. ret = security_path_mkdir(&path, next, 0);
  469. if (ret < 0)
  470. goto create_error;
  471. start = jiffies;
  472. ret = vfs_mkdir(d_inode(dir), next, 0);
  473. cachefiles_hist(cachefiles_mkdir_histogram, start);
  474. if (ret < 0)
  475. goto create_error;
  476. ASSERT(d_backing_inode(next));
  477. _debug("mkdir -> %p{%p{ino=%lu}}",
  478. next, d_backing_inode(next), d_backing_inode(next)->i_ino);
  479. } else if (!d_can_lookup(next)) {
  480. pr_err("inode %lu is not a directory\n",
  481. d_backing_inode(next)->i_ino);
  482. ret = -ENOBUFS;
  483. goto error;
  484. }
  485. } else {
  486. /* non-index objects start out life as files */
  487. if (d_is_negative(next)) {
  488. ret = cachefiles_has_space(cache, 1, 0);
  489. if (ret < 0)
  490. goto no_space_error;
  491. path.dentry = dir;
  492. ret = security_path_mknod(&path, next, S_IFREG, 0);
  493. if (ret < 0)
  494. goto create_error;
  495. start = jiffies;
  496. ret = vfs_create(d_inode(dir), next, S_IFREG, true);
  497. cachefiles_hist(cachefiles_create_histogram, start);
  498. if (ret < 0)
  499. goto create_error;
  500. ASSERT(d_backing_inode(next));
  501. _debug("create -> %p{%p{ino=%lu}}",
  502. next, d_backing_inode(next), d_backing_inode(next)->i_ino);
  503. } else if (!d_can_lookup(next) &&
  504. !d_is_reg(next)
  505. ) {
  506. pr_err("inode %lu is not a file or directory\n",
  507. d_backing_inode(next)->i_ino);
  508. ret = -ENOBUFS;
  509. goto error;
  510. }
  511. }
  512. /* process the next component */
  513. if (key) {
  514. _debug("advance");
  515. inode_unlock(d_inode(dir));
  516. dput(dir);
  517. dir = next;
  518. next = NULL;
  519. goto advance;
  520. }
  521. /* we've found the object we were looking for */
  522. object->dentry = next;
  523. /* if we've found that the terminal object exists, then we need to
  524. * check its attributes and delete it if it's out of date */
  525. if (!object->new) {
  526. _debug("validate '%pd'", next);
  527. ret = cachefiles_check_object_xattr(object, auxdata);
  528. if (ret == -ESTALE) {
  529. /* delete the object (the deleter drops the directory
  530. * mutex) */
  531. object->dentry = NULL;
  532. ret = cachefiles_bury_object(cache, dir, next, true,
  533. FSCACHE_OBJECT_IS_STALE);
  534. dput(next);
  535. next = NULL;
  536. if (ret < 0)
  537. goto delete_error;
  538. _debug("redo lookup");
  539. fscache_object_retrying_stale(&object->fscache);
  540. goto lookup_again;
  541. }
  542. }
  543. /* note that we're now using this object */
  544. ret = cachefiles_mark_object_active(cache, object);
  545. inode_unlock(d_inode(dir));
  546. dput(dir);
  547. dir = NULL;
  548. if (ret == -ETIMEDOUT)
  549. goto mark_active_timed_out;
  550. _debug("=== OBTAINED_OBJECT ===");
  551. if (object->new) {
  552. /* attach data to a newly constructed terminal object */
  553. ret = cachefiles_set_object_xattr(object, auxdata);
  554. if (ret < 0)
  555. goto check_error;
  556. } else {
  557. /* always update the atime on an object we've just looked up
  558. * (this is used to keep track of culling, and atimes are only
  559. * updated by read, write and readdir but not lookup or
  560. * open) */
  561. path.dentry = next;
  562. touch_atime(&path);
  563. }
  564. /* open a file interface onto a data file */
  565. if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
  566. if (d_is_reg(object->dentry)) {
  567. const struct address_space_operations *aops;
  568. ret = -EPERM;
  569. aops = d_backing_inode(object->dentry)->i_mapping->a_ops;
  570. if (!aops->bmap)
  571. goto check_error;
  572. if (object->dentry->d_sb->s_blocksize > PAGE_SIZE)
  573. goto check_error;
  574. object->backer = object->dentry;
  575. } else {
  576. BUG(); // TODO: open file in data-class subdir
  577. }
  578. }
  579. object->new = 0;
  580. fscache_obtained_object(&object->fscache);
  581. _leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
  582. return 0;
  583. no_space_error:
  584. fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
  585. create_error:
  586. _debug("create error %d", ret);
  587. if (ret == -EIO)
  588. cachefiles_io_error(cache, "Create/mkdir failed");
  589. goto error;
  590. mark_active_timed_out:
  591. _debug("mark active timed out");
  592. goto release_dentry;
  593. check_error:
  594. _debug("check error %d", ret);
  595. cachefiles_mark_object_inactive(
  596. cache, object, d_backing_inode(object->dentry)->i_blocks);
  597. release_dentry:
  598. dput(object->dentry);
  599. object->dentry = NULL;
  600. goto error_out;
  601. delete_error:
  602. _debug("delete error %d", ret);
  603. goto error_out2;
  604. lookup_error:
  605. _debug("lookup error %ld", PTR_ERR(next));
  606. ret = PTR_ERR(next);
  607. if (ret == -EIO)
  608. cachefiles_io_error(cache, "Lookup failed");
  609. next = NULL;
  610. error:
  611. inode_unlock(d_inode(dir));
  612. dput(next);
  613. error_out2:
  614. dput(dir);
  615. error_out:
  616. _leave(" = error %d", -ret);
  617. return ret;
  618. }
  619. /*
  620. * get a subdirectory
  621. */
  622. struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  623. struct dentry *dir,
  624. const char *dirname)
  625. {
  626. struct dentry *subdir;
  627. unsigned long start;
  628. struct path path;
  629. int ret;
  630. _enter(",,%s", dirname);
  631. /* search the current directory for the element name */
  632. inode_lock(d_inode(dir));
  633. start = jiffies;
  634. subdir = lookup_one_len(dirname, dir, strlen(dirname));
  635. cachefiles_hist(cachefiles_lookup_histogram, start);
  636. if (IS_ERR(subdir)) {
  637. if (PTR_ERR(subdir) == -ENOMEM)
  638. goto nomem_d_alloc;
  639. goto lookup_error;
  640. }
  641. _debug("subdir -> %p %s",
  642. subdir, d_backing_inode(subdir) ? "positive" : "negative");
  643. /* we need to create the subdir if it doesn't exist yet */
  644. if (d_is_negative(subdir)) {
  645. ret = cachefiles_has_space(cache, 1, 0);
  646. if (ret < 0)
  647. goto mkdir_error;
  648. _debug("attempt mkdir");
  649. path.mnt = cache->mnt;
  650. path.dentry = dir;
  651. ret = security_path_mkdir(&path, subdir, 0700);
  652. if (ret < 0)
  653. goto mkdir_error;
  654. ret = vfs_mkdir(d_inode(dir), subdir, 0700);
  655. if (ret < 0)
  656. goto mkdir_error;
  657. ASSERT(d_backing_inode(subdir));
  658. _debug("mkdir -> %p{%p{ino=%lu}}",
  659. subdir,
  660. d_backing_inode(subdir),
  661. d_backing_inode(subdir)->i_ino);
  662. }
  663. inode_unlock(d_inode(dir));
  664. /* we need to make sure the subdir is a directory */
  665. ASSERT(d_backing_inode(subdir));
  666. if (!d_can_lookup(subdir)) {
  667. pr_err("%s is not a directory\n", dirname);
  668. ret = -EIO;
  669. goto check_error;
  670. }
  671. ret = -EPERM;
  672. if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) ||
  673. !d_backing_inode(subdir)->i_op->lookup ||
  674. !d_backing_inode(subdir)->i_op->mkdir ||
  675. !d_backing_inode(subdir)->i_op->create ||
  676. !d_backing_inode(subdir)->i_op->rename ||
  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. }