namei.c 19 KB

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