namei.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 "internal.h"
  22. static int cachefiles_wait_bit(void *flags)
  23. {
  24. schedule();
  25. return 0;
  26. }
  27. #define CACHEFILES_KEYBUF_SIZE 512
  28. /*
  29. * dump debugging info about an object
  30. */
  31. static noinline
  32. void __cachefiles_printk_object(struct cachefiles_object *object,
  33. const char *prefix,
  34. u8 *keybuf)
  35. {
  36. struct fscache_cookie *cookie;
  37. unsigned keylen, loop;
  38. printk(KERN_ERR "%sobject: OBJ%x\n",
  39. prefix, object->fscache.debug_id);
  40. printk(KERN_ERR "%sobjstate=%s fl=%lx swfl=%lx ev=%lx[%lx]\n",
  41. prefix, fscache_object_states[object->fscache.state],
  42. object->fscache.flags, object->fscache.work.flags,
  43. object->fscache.events,
  44. object->fscache.event_mask & FSCACHE_OBJECT_EVENTS_MASK);
  45. printk(KERN_ERR "%sops=%u inp=%u exc=%u\n",
  46. prefix, object->fscache.n_ops, object->fscache.n_in_progress,
  47. object->fscache.n_exclusive);
  48. printk(KERN_ERR "%sparent=%p\n",
  49. prefix, object->fscache.parent);
  50. spin_lock(&object->fscache.lock);
  51. cookie = object->fscache.cookie;
  52. if (cookie) {
  53. printk(KERN_ERR "%scookie=%p [pr=%p nd=%p fl=%lx]\n",
  54. prefix,
  55. object->fscache.cookie,
  56. object->fscache.cookie->parent,
  57. object->fscache.cookie->netfs_data,
  58. object->fscache.cookie->flags);
  59. if (keybuf)
  60. keylen = cookie->def->get_key(cookie->netfs_data, keybuf,
  61. CACHEFILES_KEYBUF_SIZE);
  62. else
  63. keylen = 0;
  64. } else {
  65. printk(KERN_ERR "%scookie=NULL\n", prefix);
  66. keylen = 0;
  67. }
  68. spin_unlock(&object->fscache.lock);
  69. if (keylen) {
  70. printk(KERN_ERR "%skey=[%u] '", prefix, keylen);
  71. for (loop = 0; loop < keylen; loop++)
  72. printk("%02x", keybuf[loop]);
  73. printk("'\n");
  74. }
  75. }
  76. /*
  77. * dump debugging info about a pair of objects
  78. */
  79. static noinline void cachefiles_printk_object(struct cachefiles_object *object,
  80. struct cachefiles_object *xobject)
  81. {
  82. u8 *keybuf;
  83. keybuf = kmalloc(CACHEFILES_KEYBUF_SIZE, GFP_NOIO);
  84. if (object)
  85. __cachefiles_printk_object(object, "", keybuf);
  86. if (xobject)
  87. __cachefiles_printk_object(xobject, "x", keybuf);
  88. kfree(keybuf);
  89. }
  90. /*
  91. * record the fact that an object is now active
  92. */
  93. static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
  94. struct cachefiles_object *object)
  95. {
  96. struct cachefiles_object *xobject;
  97. struct rb_node **_p, *_parent = NULL;
  98. struct dentry *dentry;
  99. _enter(",%p", object);
  100. try_again:
  101. write_lock(&cache->active_lock);
  102. if (test_and_set_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) {
  103. printk(KERN_ERR "CacheFiles: Error: Object already active\n");
  104. cachefiles_printk_object(object, NULL);
  105. BUG();
  106. }
  107. dentry = object->dentry;
  108. _p = &cache->active_nodes.rb_node;
  109. while (*_p) {
  110. _parent = *_p;
  111. xobject = rb_entry(_parent,
  112. struct cachefiles_object, active_node);
  113. ASSERT(xobject != object);
  114. if (xobject->dentry > dentry)
  115. _p = &(*_p)->rb_left;
  116. else if (xobject->dentry < dentry)
  117. _p = &(*_p)->rb_right;
  118. else
  119. goto wait_for_old_object;
  120. }
  121. rb_link_node(&object->active_node, _parent, _p);
  122. rb_insert_color(&object->active_node, &cache->active_nodes);
  123. write_unlock(&cache->active_lock);
  124. _leave("");
  125. return;
  126. /* an old object from a previous incarnation is hogging the slot - we
  127. * need to wait for it to be destroyed */
  128. wait_for_old_object:
  129. if (xobject->fscache.state < FSCACHE_OBJECT_DYING) {
  130. printk(KERN_ERR "\n");
  131. printk(KERN_ERR "CacheFiles: Error:"
  132. " Unexpected object collision\n");
  133. cachefiles_printk_object(object, xobject);
  134. BUG();
  135. }
  136. atomic_inc(&xobject->usage);
  137. write_unlock(&cache->active_lock);
  138. _debug(">>> wait");
  139. wait_on_bit(&xobject->flags, CACHEFILES_OBJECT_ACTIVE,
  140. cachefiles_wait_bit, TASK_UNINTERRUPTIBLE);
  141. _debug("<<< waited");
  142. cache->cache.ops->put_object(&xobject->fscache);
  143. goto try_again;
  144. }
  145. /*
  146. * delete an object representation from the cache
  147. * - file backed objects are unlinked
  148. * - directory backed objects are stuffed into the graveyard for userspace to
  149. * delete
  150. * - unlocks the directory mutex
  151. */
  152. static int cachefiles_bury_object(struct cachefiles_cache *cache,
  153. struct dentry *dir,
  154. struct dentry *rep)
  155. {
  156. struct dentry *grave, *trap;
  157. char nbuffer[8 + 8 + 1];
  158. int ret;
  159. _enter(",'%*.*s','%*.*s'",
  160. dir->d_name.len, dir->d_name.len, dir->d_name.name,
  161. rep->d_name.len, rep->d_name.len, rep->d_name.name);
  162. /* non-directories can just be unlinked */
  163. if (!S_ISDIR(rep->d_inode->i_mode)) {
  164. _debug("unlink stale object");
  165. ret = vfs_unlink(dir->d_inode, rep);
  166. mutex_unlock(&dir->d_inode->i_mutex);
  167. if (ret == -EIO)
  168. cachefiles_io_error(cache, "Unlink failed");
  169. _leave(" = %d", ret);
  170. return ret;
  171. }
  172. /* directories have to be moved to the graveyard */
  173. _debug("move stale object to graveyard");
  174. mutex_unlock(&dir->d_inode->i_mutex);
  175. try_again:
  176. /* first step is to make up a grave dentry in the graveyard */
  177. sprintf(nbuffer, "%08x%08x",
  178. (uint32_t) get_seconds(),
  179. (uint32_t) atomic_inc_return(&cache->gravecounter));
  180. /* do the multiway lock magic */
  181. trap = lock_rename(cache->graveyard, dir);
  182. /* do some checks before getting the grave dentry */
  183. if (rep->d_parent != dir) {
  184. /* the entry was probably culled when we dropped the parent dir
  185. * lock */
  186. unlock_rename(cache->graveyard, dir);
  187. _leave(" = 0 [culled?]");
  188. return 0;
  189. }
  190. if (!S_ISDIR(cache->graveyard->d_inode->i_mode)) {
  191. unlock_rename(cache->graveyard, dir);
  192. cachefiles_io_error(cache, "Graveyard no longer a directory");
  193. return -EIO;
  194. }
  195. if (trap == rep) {
  196. unlock_rename(cache->graveyard, dir);
  197. cachefiles_io_error(cache, "May not make directory loop");
  198. return -EIO;
  199. }
  200. if (d_mountpoint(rep)) {
  201. unlock_rename(cache->graveyard, dir);
  202. cachefiles_io_error(cache, "Mountpoint in cache");
  203. return -EIO;
  204. }
  205. grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer));
  206. if (IS_ERR(grave)) {
  207. unlock_rename(cache->graveyard, dir);
  208. if (PTR_ERR(grave) == -ENOMEM) {
  209. _leave(" = -ENOMEM");
  210. return -ENOMEM;
  211. }
  212. cachefiles_io_error(cache, "Lookup error %ld",
  213. PTR_ERR(grave));
  214. return -EIO;
  215. }
  216. if (grave->d_inode) {
  217. unlock_rename(cache->graveyard, dir);
  218. dput(grave);
  219. grave = NULL;
  220. cond_resched();
  221. goto try_again;
  222. }
  223. if (d_mountpoint(grave)) {
  224. unlock_rename(cache->graveyard, dir);
  225. dput(grave);
  226. cachefiles_io_error(cache, "Mountpoint in graveyard");
  227. return -EIO;
  228. }
  229. /* target should not be an ancestor of source */
  230. if (trap == grave) {
  231. unlock_rename(cache->graveyard, dir);
  232. dput(grave);
  233. cachefiles_io_error(cache, "May not make directory loop");
  234. return -EIO;
  235. }
  236. /* attempt the rename */
  237. ret = vfs_rename(dir->d_inode, rep, cache->graveyard->d_inode, grave);
  238. if (ret != 0 && ret != -ENOMEM)
  239. cachefiles_io_error(cache, "Rename failed with error %d", ret);
  240. unlock_rename(cache->graveyard, dir);
  241. dput(grave);
  242. _leave(" = 0");
  243. return 0;
  244. }
  245. /*
  246. * delete an object representation from the cache
  247. */
  248. int cachefiles_delete_object(struct cachefiles_cache *cache,
  249. struct cachefiles_object *object)
  250. {
  251. struct dentry *dir;
  252. int ret;
  253. _enter(",{%p}", object->dentry);
  254. ASSERT(object->dentry);
  255. ASSERT(object->dentry->d_inode);
  256. ASSERT(object->dentry->d_parent);
  257. dir = dget_parent(object->dentry);
  258. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  259. ret = cachefiles_bury_object(cache, dir, object->dentry);
  260. dput(dir);
  261. _leave(" = %d", ret);
  262. return ret;
  263. }
  264. /*
  265. * walk from the parent object to the child object through the backing
  266. * filesystem, creating directories as we go
  267. */
  268. int cachefiles_walk_to_object(struct cachefiles_object *parent,
  269. struct cachefiles_object *object,
  270. const char *key,
  271. struct cachefiles_xattr *auxdata)
  272. {
  273. struct cachefiles_cache *cache;
  274. struct dentry *dir, *next = NULL;
  275. unsigned long start;
  276. const char *name;
  277. int ret, nlen;
  278. _enter("{%p},,%s,", parent->dentry, key);
  279. cache = container_of(parent->fscache.cache,
  280. struct cachefiles_cache, cache);
  281. ASSERT(parent->dentry);
  282. ASSERT(parent->dentry->d_inode);
  283. if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
  284. // TODO: convert file to dir
  285. _leave("looking up in none directory");
  286. return -ENOBUFS;
  287. }
  288. dir = dget(parent->dentry);
  289. advance:
  290. /* attempt to transit the first directory component */
  291. name = key;
  292. nlen = strlen(key);
  293. /* key ends in a double NUL */
  294. key = key + nlen + 1;
  295. if (!*key)
  296. key = NULL;
  297. lookup_again:
  298. /* search the current directory for the element name */
  299. _debug("lookup '%s'", name);
  300. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  301. start = jiffies;
  302. next = lookup_one_len(name, dir, nlen);
  303. cachefiles_hist(cachefiles_lookup_histogram, start);
  304. if (IS_ERR(next))
  305. goto lookup_error;
  306. _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
  307. if (!key)
  308. object->new = !next->d_inode;
  309. /* if this element of the path doesn't exist, then the lookup phase
  310. * failed, and we can release any readers in the certain knowledge that
  311. * there's nothing for them to actually read */
  312. if (!next->d_inode)
  313. fscache_object_lookup_negative(&object->fscache);
  314. /* we need to create the object if it's negative */
  315. if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
  316. /* index objects and intervening tree levels must be subdirs */
  317. if (!next->d_inode) {
  318. ret = cachefiles_has_space(cache, 1, 0);
  319. if (ret < 0)
  320. goto create_error;
  321. start = jiffies;
  322. ret = vfs_mkdir(dir->d_inode, next, 0);
  323. cachefiles_hist(cachefiles_mkdir_histogram, start);
  324. if (ret < 0)
  325. goto create_error;
  326. ASSERT(next->d_inode);
  327. _debug("mkdir -> %p{%p{ino=%lu}}",
  328. next, next->d_inode, next->d_inode->i_ino);
  329. } else if (!S_ISDIR(next->d_inode->i_mode)) {
  330. kerror("inode %lu is not a directory",
  331. next->d_inode->i_ino);
  332. ret = -ENOBUFS;
  333. goto error;
  334. }
  335. } else {
  336. /* non-index objects start out life as files */
  337. if (!next->d_inode) {
  338. ret = cachefiles_has_space(cache, 1, 0);
  339. if (ret < 0)
  340. goto create_error;
  341. start = jiffies;
  342. ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
  343. cachefiles_hist(cachefiles_create_histogram, start);
  344. if (ret < 0)
  345. goto create_error;
  346. ASSERT(next->d_inode);
  347. _debug("create -> %p{%p{ino=%lu}}",
  348. next, next->d_inode, next->d_inode->i_ino);
  349. } else if (!S_ISDIR(next->d_inode->i_mode) &&
  350. !S_ISREG(next->d_inode->i_mode)
  351. ) {
  352. kerror("inode %lu is not a file or directory",
  353. next->d_inode->i_ino);
  354. ret = -ENOBUFS;
  355. goto error;
  356. }
  357. }
  358. /* process the next component */
  359. if (key) {
  360. _debug("advance");
  361. mutex_unlock(&dir->d_inode->i_mutex);
  362. dput(dir);
  363. dir = next;
  364. next = NULL;
  365. goto advance;
  366. }
  367. /* we've found the object we were looking for */
  368. object->dentry = next;
  369. /* if we've found that the terminal object exists, then we need to
  370. * check its attributes and delete it if it's out of date */
  371. if (!object->new) {
  372. _debug("validate '%*.*s'",
  373. next->d_name.len, next->d_name.len, next->d_name.name);
  374. ret = cachefiles_check_object_xattr(object, auxdata);
  375. if (ret == -ESTALE) {
  376. /* delete the object (the deleter drops the directory
  377. * mutex) */
  378. object->dentry = NULL;
  379. ret = cachefiles_bury_object(cache, dir, next);
  380. dput(next);
  381. next = NULL;
  382. if (ret < 0)
  383. goto delete_error;
  384. _debug("redo lookup");
  385. goto lookup_again;
  386. }
  387. }
  388. /* note that we're now using this object */
  389. cachefiles_mark_object_active(cache, object);
  390. mutex_unlock(&dir->d_inode->i_mutex);
  391. dput(dir);
  392. dir = NULL;
  393. _debug("=== OBTAINED_OBJECT ===");
  394. if (object->new) {
  395. /* attach data to a newly constructed terminal object */
  396. ret = cachefiles_set_object_xattr(object, auxdata);
  397. if (ret < 0)
  398. goto check_error;
  399. } else {
  400. /* always update the atime on an object we've just looked up
  401. * (this is used to keep track of culling, and atimes are only
  402. * updated by read, write and readdir but not lookup or
  403. * open) */
  404. touch_atime(cache->mnt, next);
  405. }
  406. /* open a file interface onto a data file */
  407. if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
  408. if (S_ISREG(object->dentry->d_inode->i_mode)) {
  409. const struct address_space_operations *aops;
  410. ret = -EPERM;
  411. aops = object->dentry->d_inode->i_mapping->a_ops;
  412. if (!aops->bmap)
  413. goto check_error;
  414. object->backer = object->dentry;
  415. } else {
  416. BUG(); // TODO: open file in data-class subdir
  417. }
  418. }
  419. object->new = 0;
  420. fscache_obtained_object(&object->fscache);
  421. _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
  422. return 0;
  423. create_error:
  424. _debug("create error %d", ret);
  425. if (ret == -EIO)
  426. cachefiles_io_error(cache, "Create/mkdir failed");
  427. goto error;
  428. check_error:
  429. _debug("check error %d", ret);
  430. write_lock(&cache->active_lock);
  431. rb_erase(&object->active_node, &cache->active_nodes);
  432. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  433. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  434. write_unlock(&cache->active_lock);
  435. dput(object->dentry);
  436. object->dentry = NULL;
  437. goto error_out;
  438. delete_error:
  439. _debug("delete error %d", ret);
  440. goto error_out2;
  441. lookup_error:
  442. _debug("lookup error %ld", PTR_ERR(next));
  443. ret = PTR_ERR(next);
  444. if (ret == -EIO)
  445. cachefiles_io_error(cache, "Lookup failed");
  446. next = NULL;
  447. error:
  448. mutex_unlock(&dir->d_inode->i_mutex);
  449. dput(next);
  450. error_out2:
  451. dput(dir);
  452. error_out:
  453. if (ret == -ENOSPC)
  454. ret = -ENOBUFS;
  455. _leave(" = error %d", -ret);
  456. return ret;
  457. }
  458. /*
  459. * get a subdirectory
  460. */
  461. struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  462. struct dentry *dir,
  463. const char *dirname)
  464. {
  465. struct dentry *subdir;
  466. unsigned long start;
  467. int ret;
  468. _enter(",,%s", dirname);
  469. /* search the current directory for the element name */
  470. mutex_lock(&dir->d_inode->i_mutex);
  471. start = jiffies;
  472. subdir = lookup_one_len(dirname, dir, strlen(dirname));
  473. cachefiles_hist(cachefiles_lookup_histogram, start);
  474. if (IS_ERR(subdir)) {
  475. if (PTR_ERR(subdir) == -ENOMEM)
  476. goto nomem_d_alloc;
  477. goto lookup_error;
  478. }
  479. _debug("subdir -> %p %s",
  480. subdir, subdir->d_inode ? "positive" : "negative");
  481. /* we need to create the subdir if it doesn't exist yet */
  482. if (!subdir->d_inode) {
  483. ret = cachefiles_has_space(cache, 1, 0);
  484. if (ret < 0)
  485. goto mkdir_error;
  486. _debug("attempt mkdir");
  487. ret = vfs_mkdir(dir->d_inode, subdir, 0700);
  488. if (ret < 0)
  489. goto mkdir_error;
  490. ASSERT(subdir->d_inode);
  491. _debug("mkdir -> %p{%p{ino=%lu}}",
  492. subdir,
  493. subdir->d_inode,
  494. subdir->d_inode->i_ino);
  495. }
  496. mutex_unlock(&dir->d_inode->i_mutex);
  497. /* we need to make sure the subdir is a directory */
  498. ASSERT(subdir->d_inode);
  499. if (!S_ISDIR(subdir->d_inode->i_mode)) {
  500. kerror("%s is not a directory", dirname);
  501. ret = -EIO;
  502. goto check_error;
  503. }
  504. ret = -EPERM;
  505. if (!subdir->d_inode->i_op ||
  506. !subdir->d_inode->i_op->setxattr ||
  507. !subdir->d_inode->i_op->getxattr ||
  508. !subdir->d_inode->i_op->lookup ||
  509. !subdir->d_inode->i_op->mkdir ||
  510. !subdir->d_inode->i_op->create ||
  511. !subdir->d_inode->i_op->rename ||
  512. !subdir->d_inode->i_op->rmdir ||
  513. !subdir->d_inode->i_op->unlink)
  514. goto check_error;
  515. _leave(" = [%lu]", subdir->d_inode->i_ino);
  516. return subdir;
  517. check_error:
  518. dput(subdir);
  519. _leave(" = %d [check]", ret);
  520. return ERR_PTR(ret);
  521. mkdir_error:
  522. mutex_unlock(&dir->d_inode->i_mutex);
  523. dput(subdir);
  524. kerror("mkdir %s failed with error %d", dirname, ret);
  525. return ERR_PTR(ret);
  526. lookup_error:
  527. mutex_unlock(&dir->d_inode->i_mutex);
  528. ret = PTR_ERR(subdir);
  529. kerror("Lookup %s failed with error %d", dirname, ret);
  530. return ERR_PTR(ret);
  531. nomem_d_alloc:
  532. mutex_unlock(&dir->d_inode->i_mutex);
  533. _leave(" = -ENOMEM");
  534. return ERR_PTR(-ENOMEM);
  535. }
  536. /*
  537. * find out if an object is in use or not
  538. * - if finds object and it's not in use:
  539. * - returns a pointer to the object and a reference on it
  540. * - returns with the directory locked
  541. */
  542. static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
  543. struct dentry *dir,
  544. char *filename)
  545. {
  546. struct cachefiles_object *object;
  547. struct rb_node *_n;
  548. struct dentry *victim;
  549. unsigned long start;
  550. int ret;
  551. //_enter(",%*.*s/,%s",
  552. // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
  553. /* look up the victim */
  554. mutex_lock_nested(&dir->d_inode->i_mutex, 1);
  555. start = jiffies;
  556. victim = lookup_one_len(filename, dir, strlen(filename));
  557. cachefiles_hist(cachefiles_lookup_histogram, start);
  558. if (IS_ERR(victim))
  559. goto lookup_error;
  560. //_debug("victim -> %p %s",
  561. // victim, victim->d_inode ? "positive" : "negative");
  562. /* if the object is no longer there then we probably retired the object
  563. * at the netfs's request whilst the cull was in progress
  564. */
  565. if (!victim->d_inode) {
  566. mutex_unlock(&dir->d_inode->i_mutex);
  567. dput(victim);
  568. _leave(" = -ENOENT [absent]");
  569. return ERR_PTR(-ENOENT);
  570. }
  571. /* check to see if we're using this object */
  572. read_lock(&cache->active_lock);
  573. _n = cache->active_nodes.rb_node;
  574. while (_n) {
  575. object = rb_entry(_n, struct cachefiles_object, active_node);
  576. if (object->dentry > victim)
  577. _n = _n->rb_left;
  578. else if (object->dentry < victim)
  579. _n = _n->rb_right;
  580. else
  581. goto object_in_use;
  582. }
  583. read_unlock(&cache->active_lock);
  584. //_leave(" = %p", victim);
  585. return victim;
  586. object_in_use:
  587. read_unlock(&cache->active_lock);
  588. mutex_unlock(&dir->d_inode->i_mutex);
  589. dput(victim);
  590. //_leave(" = -EBUSY [in use]");
  591. return ERR_PTR(-EBUSY);
  592. lookup_error:
  593. mutex_unlock(&dir->d_inode->i_mutex);
  594. ret = PTR_ERR(victim);
  595. if (ret == -ENOENT) {
  596. /* file or dir now absent - probably retired by netfs */
  597. _leave(" = -ESTALE [absent]");
  598. return ERR_PTR(-ESTALE);
  599. }
  600. if (ret == -EIO) {
  601. cachefiles_io_error(cache, "Lookup failed");
  602. } else if (ret != -ENOMEM) {
  603. kerror("Internal error: %d", ret);
  604. ret = -EIO;
  605. }
  606. _leave(" = %d", ret);
  607. return ERR_PTR(ret);
  608. }
  609. /*
  610. * cull an object if it's not in use
  611. * - called only by cache manager daemon
  612. */
  613. int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  614. char *filename)
  615. {
  616. struct dentry *victim;
  617. int ret;
  618. _enter(",%*.*s/,%s",
  619. dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
  620. victim = cachefiles_check_active(cache, dir, filename);
  621. if (IS_ERR(victim))
  622. return PTR_ERR(victim);
  623. _debug("victim -> %p %s",
  624. victim, victim->d_inode ? "positive" : "negative");
  625. /* okay... the victim is not being used so we can cull it
  626. * - start by marking it as stale
  627. */
  628. _debug("victim is cullable");
  629. ret = cachefiles_remove_object_xattr(cache, victim);
  630. if (ret < 0)
  631. goto error_unlock;
  632. /* actually remove the victim (drops the dir mutex) */
  633. _debug("bury");
  634. ret = cachefiles_bury_object(cache, dir, victim);
  635. if (ret < 0)
  636. goto error;
  637. dput(victim);
  638. _leave(" = 0");
  639. return 0;
  640. error_unlock:
  641. mutex_unlock(&dir->d_inode->i_mutex);
  642. error:
  643. dput(victim);
  644. if (ret == -ENOENT) {
  645. /* file or dir now absent - probably retired by netfs */
  646. _leave(" = -ESTALE [absent]");
  647. return -ESTALE;
  648. }
  649. if (ret != -ENOMEM) {
  650. kerror("Internal error: %d", ret);
  651. ret = -EIO;
  652. }
  653. _leave(" = %d", ret);
  654. return ret;
  655. }
  656. /*
  657. * find out if an object is in use or not
  658. * - called only by cache manager daemon
  659. * - returns -EBUSY or 0 to indicate whether an object is in use or not
  660. */
  661. int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
  662. char *filename)
  663. {
  664. struct dentry *victim;
  665. //_enter(",%*.*s/,%s",
  666. // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
  667. victim = cachefiles_check_active(cache, dir, filename);
  668. if (IS_ERR(victim))
  669. return PTR_ERR(victim);
  670. mutex_unlock(&dir->d_inode->i_mutex);
  671. dput(victim);
  672. //_leave(" = 0");
  673. return 0;
  674. }