namei.c 24 KB

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