namei.c 22 KB

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