namei.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. mutex_unlock(&d_inode(dir)->i_mutex);
  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. mutex_unlock(&d_inode(dir)->i_mutex);
  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. mutex_lock_nested(&d_inode(dir)->i_mutex, 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. mutex_unlock(&d_inode(dir)->i_mutex);
  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. mutex_unlock(&d_inode(dir)->i_mutex);
  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. mutex_lock_nested(&d_inode(dir)->i_mutex, 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. mutex_unlock(&d_inode(dir)->i_mutex);
  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. mutex_unlock(&d_inode(dir)->i_mutex);
  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. object->backer = object->dentry;
  553. } else {
  554. BUG(); // TODO: open file in data-class subdir
  555. }
  556. }
  557. object->new = 0;
  558. fscache_obtained_object(&object->fscache);
  559. _leave(" = 0 [%lu]", d_backing_inode(object->dentry)->i_ino);
  560. return 0;
  561. no_space_error:
  562. fscache_object_mark_killed(&object->fscache, FSCACHE_OBJECT_NO_SPACE);
  563. create_error:
  564. _debug("create error %d", ret);
  565. if (ret == -EIO)
  566. cachefiles_io_error(cache, "Create/mkdir failed");
  567. goto error;
  568. mark_active_timed_out:
  569. _debug("mark active timed out");
  570. goto release_dentry;
  571. check_error:
  572. _debug("check error %d", ret);
  573. write_lock(&cache->active_lock);
  574. rb_erase(&object->active_node, &cache->active_nodes);
  575. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  576. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  577. write_unlock(&cache->active_lock);
  578. release_dentry:
  579. dput(object->dentry);
  580. object->dentry = NULL;
  581. goto error_out;
  582. delete_error:
  583. _debug("delete error %d", ret);
  584. goto error_out2;
  585. lookup_error:
  586. _debug("lookup error %ld", PTR_ERR(next));
  587. ret = PTR_ERR(next);
  588. if (ret == -EIO)
  589. cachefiles_io_error(cache, "Lookup failed");
  590. next = NULL;
  591. error:
  592. mutex_unlock(&d_inode(dir)->i_mutex);
  593. dput(next);
  594. error_out2:
  595. dput(dir);
  596. error_out:
  597. _leave(" = error %d", -ret);
  598. return ret;
  599. }
  600. /*
  601. * get a subdirectory
  602. */
  603. struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  604. struct dentry *dir,
  605. const char *dirname)
  606. {
  607. struct dentry *subdir;
  608. unsigned long start;
  609. struct path path;
  610. int ret;
  611. _enter(",,%s", dirname);
  612. /* search the current directory for the element name */
  613. mutex_lock(&d_inode(dir)->i_mutex);
  614. start = jiffies;
  615. subdir = lookup_one_len(dirname, dir, strlen(dirname));
  616. cachefiles_hist(cachefiles_lookup_histogram, start);
  617. if (IS_ERR(subdir)) {
  618. if (PTR_ERR(subdir) == -ENOMEM)
  619. goto nomem_d_alloc;
  620. goto lookup_error;
  621. }
  622. _debug("subdir -> %p %s",
  623. subdir, d_backing_inode(subdir) ? "positive" : "negative");
  624. /* we need to create the subdir if it doesn't exist yet */
  625. if (d_is_negative(subdir)) {
  626. ret = cachefiles_has_space(cache, 1, 0);
  627. if (ret < 0)
  628. goto mkdir_error;
  629. _debug("attempt mkdir");
  630. path.mnt = cache->mnt;
  631. path.dentry = dir;
  632. ret = security_path_mkdir(&path, subdir, 0700);
  633. if (ret < 0)
  634. goto mkdir_error;
  635. ret = vfs_mkdir(d_inode(dir), subdir, 0700);
  636. if (ret < 0)
  637. goto mkdir_error;
  638. ASSERT(d_backing_inode(subdir));
  639. _debug("mkdir -> %p{%p{ino=%lu}}",
  640. subdir,
  641. d_backing_inode(subdir),
  642. d_backing_inode(subdir)->i_ino);
  643. }
  644. mutex_unlock(&d_inode(dir)->i_mutex);
  645. /* we need to make sure the subdir is a directory */
  646. ASSERT(d_backing_inode(subdir));
  647. if (!d_can_lookup(subdir)) {
  648. pr_err("%s is not a directory\n", dirname);
  649. ret = -EIO;
  650. goto check_error;
  651. }
  652. ret = -EPERM;
  653. if (!d_backing_inode(subdir)->i_op->setxattr ||
  654. !d_backing_inode(subdir)->i_op->getxattr ||
  655. !d_backing_inode(subdir)->i_op->lookup ||
  656. !d_backing_inode(subdir)->i_op->mkdir ||
  657. !d_backing_inode(subdir)->i_op->create ||
  658. (!d_backing_inode(subdir)->i_op->rename &&
  659. !d_backing_inode(subdir)->i_op->rename2) ||
  660. !d_backing_inode(subdir)->i_op->rmdir ||
  661. !d_backing_inode(subdir)->i_op->unlink)
  662. goto check_error;
  663. _leave(" = [%lu]", d_backing_inode(subdir)->i_ino);
  664. return subdir;
  665. check_error:
  666. dput(subdir);
  667. _leave(" = %d [check]", ret);
  668. return ERR_PTR(ret);
  669. mkdir_error:
  670. mutex_unlock(&d_inode(dir)->i_mutex);
  671. dput(subdir);
  672. pr_err("mkdir %s failed with error %d\n", dirname, ret);
  673. return ERR_PTR(ret);
  674. lookup_error:
  675. mutex_unlock(&d_inode(dir)->i_mutex);
  676. ret = PTR_ERR(subdir);
  677. pr_err("Lookup %s failed with error %d\n", dirname, ret);
  678. return ERR_PTR(ret);
  679. nomem_d_alloc:
  680. mutex_unlock(&d_inode(dir)->i_mutex);
  681. _leave(" = -ENOMEM");
  682. return ERR_PTR(-ENOMEM);
  683. }
  684. /*
  685. * find out if an object is in use or not
  686. * - if finds object and it's not in use:
  687. * - returns a pointer to the object and a reference on it
  688. * - returns with the directory locked
  689. */
  690. static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
  691. struct dentry *dir,
  692. char *filename)
  693. {
  694. struct cachefiles_object *object;
  695. struct rb_node *_n;
  696. struct dentry *victim;
  697. unsigned long start;
  698. int ret;
  699. //_enter(",%pd/,%s",
  700. // dir, filename);
  701. /* look up the victim */
  702. mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_PARENT);
  703. start = jiffies;
  704. victim = lookup_one_len(filename, dir, strlen(filename));
  705. cachefiles_hist(cachefiles_lookup_histogram, start);
  706. if (IS_ERR(victim))
  707. goto lookup_error;
  708. //_debug("victim -> %p %s",
  709. // victim, d_backing_inode(victim) ? "positive" : "negative");
  710. /* if the object is no longer there then we probably retired the object
  711. * at the netfs's request whilst the cull was in progress
  712. */
  713. if (d_is_negative(victim)) {
  714. mutex_unlock(&d_inode(dir)->i_mutex);
  715. dput(victim);
  716. _leave(" = -ENOENT [absent]");
  717. return ERR_PTR(-ENOENT);
  718. }
  719. /* check to see if we're using this object */
  720. read_lock(&cache->active_lock);
  721. _n = cache->active_nodes.rb_node;
  722. while (_n) {
  723. object = rb_entry(_n, struct cachefiles_object, active_node);
  724. if (object->dentry > victim)
  725. _n = _n->rb_left;
  726. else if (object->dentry < victim)
  727. _n = _n->rb_right;
  728. else
  729. goto object_in_use;
  730. }
  731. read_unlock(&cache->active_lock);
  732. //_leave(" = %p", victim);
  733. return victim;
  734. object_in_use:
  735. read_unlock(&cache->active_lock);
  736. mutex_unlock(&d_inode(dir)->i_mutex);
  737. dput(victim);
  738. //_leave(" = -EBUSY [in use]");
  739. return ERR_PTR(-EBUSY);
  740. lookup_error:
  741. mutex_unlock(&d_inode(dir)->i_mutex);
  742. ret = PTR_ERR(victim);
  743. if (ret == -ENOENT) {
  744. /* file or dir now absent - probably retired by netfs */
  745. _leave(" = -ESTALE [absent]");
  746. return ERR_PTR(-ESTALE);
  747. }
  748. if (ret == -EIO) {
  749. cachefiles_io_error(cache, "Lookup failed");
  750. } else if (ret != -ENOMEM) {
  751. pr_err("Internal error: %d\n", ret);
  752. ret = -EIO;
  753. }
  754. _leave(" = %d", ret);
  755. return ERR_PTR(ret);
  756. }
  757. /*
  758. * cull an object if it's not in use
  759. * - called only by cache manager daemon
  760. */
  761. int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  762. char *filename)
  763. {
  764. struct dentry *victim;
  765. int ret;
  766. _enter(",%pd/,%s", dir, filename);
  767. victim = cachefiles_check_active(cache, dir, filename);
  768. if (IS_ERR(victim))
  769. return PTR_ERR(victim);
  770. _debug("victim -> %p %s",
  771. victim, d_backing_inode(victim) ? "positive" : "negative");
  772. /* okay... the victim is not being used so we can cull it
  773. * - start by marking it as stale
  774. */
  775. _debug("victim is cullable");
  776. ret = cachefiles_remove_object_xattr(cache, victim);
  777. if (ret < 0)
  778. goto error_unlock;
  779. /* actually remove the victim (drops the dir mutex) */
  780. _debug("bury");
  781. ret = cachefiles_bury_object(cache, dir, victim, false,
  782. FSCACHE_OBJECT_WAS_CULLED);
  783. if (ret < 0)
  784. goto error;
  785. dput(victim);
  786. _leave(" = 0");
  787. return 0;
  788. error_unlock:
  789. mutex_unlock(&d_inode(dir)->i_mutex);
  790. error:
  791. dput(victim);
  792. if (ret == -ENOENT) {
  793. /* file or dir now absent - probably retired by netfs */
  794. _leave(" = -ESTALE [absent]");
  795. return -ESTALE;
  796. }
  797. if (ret != -ENOMEM) {
  798. pr_err("Internal error: %d\n", ret);
  799. ret = -EIO;
  800. }
  801. _leave(" = %d", ret);
  802. return ret;
  803. }
  804. /*
  805. * find out if an object is in use or not
  806. * - called only by cache manager daemon
  807. * - returns -EBUSY or 0 to indicate whether an object is in use or not
  808. */
  809. int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
  810. char *filename)
  811. {
  812. struct dentry *victim;
  813. //_enter(",%pd/,%s",
  814. // dir, filename);
  815. victim = cachefiles_check_active(cache, dir, filename);
  816. if (IS_ERR(victim))
  817. return PTR_ERR(victim);
  818. mutex_unlock(&d_inode(dir)->i_mutex);
  819. dput(victim);
  820. //_leave(" = 0");
  821. return 0;
  822. }