namei.c 22 KB

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