namei.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. ret = cachefiles_bury_object(cache, dir, object->dentry);
  299. dput(dir);
  300. _leave(" = %d", ret);
  301. return ret;
  302. }
  303. /*
  304. * walk from the parent object to the child object through the backing
  305. * filesystem, creating directories as we go
  306. */
  307. int cachefiles_walk_to_object(struct cachefiles_object *parent,
  308. struct cachefiles_object *object,
  309. const char *key,
  310. struct cachefiles_xattr *auxdata)
  311. {
  312. struct cachefiles_cache *cache;
  313. struct dentry *dir, *next = NULL;
  314. unsigned long start;
  315. const char *name;
  316. int ret, nlen;
  317. _enter("{%p},,%s,", parent->dentry, key);
  318. cache = container_of(parent->fscache.cache,
  319. struct cachefiles_cache, cache);
  320. ASSERT(parent->dentry);
  321. ASSERT(parent->dentry->d_inode);
  322. if (!(S_ISDIR(parent->dentry->d_inode->i_mode))) {
  323. // TODO: convert file to dir
  324. _leave("looking up in none directory");
  325. return -ENOBUFS;
  326. }
  327. dir = dget(parent->dentry);
  328. advance:
  329. /* attempt to transit the first directory component */
  330. name = key;
  331. nlen = strlen(key);
  332. /* key ends in a double NUL */
  333. key = key + nlen + 1;
  334. if (!*key)
  335. key = NULL;
  336. lookup_again:
  337. /* search the current directory for the element name */
  338. _debug("lookup '%s'", name);
  339. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  340. start = jiffies;
  341. next = lookup_one_len(name, dir, nlen);
  342. cachefiles_hist(cachefiles_lookup_histogram, start);
  343. if (IS_ERR(next))
  344. goto lookup_error;
  345. _debug("next -> %p %s", next, next->d_inode ? "positive" : "negative");
  346. if (!key)
  347. object->new = !next->d_inode;
  348. /* if this element of the path doesn't exist, then the lookup phase
  349. * failed, and we can release any readers in the certain knowledge that
  350. * there's nothing for them to actually read */
  351. if (!next->d_inode)
  352. fscache_object_lookup_negative(&object->fscache);
  353. /* we need to create the object if it's negative */
  354. if (key || object->type == FSCACHE_COOKIE_TYPE_INDEX) {
  355. /* index objects and intervening tree levels must be subdirs */
  356. if (!next->d_inode) {
  357. ret = cachefiles_has_space(cache, 1, 0);
  358. if (ret < 0)
  359. goto create_error;
  360. start = jiffies;
  361. ret = vfs_mkdir(dir->d_inode, next, 0);
  362. cachefiles_hist(cachefiles_mkdir_histogram, start);
  363. if (ret < 0)
  364. goto create_error;
  365. ASSERT(next->d_inode);
  366. _debug("mkdir -> %p{%p{ino=%lu}}",
  367. next, next->d_inode, next->d_inode->i_ino);
  368. } else if (!S_ISDIR(next->d_inode->i_mode)) {
  369. kerror("inode %lu is not a directory",
  370. next->d_inode->i_ino);
  371. ret = -ENOBUFS;
  372. goto error;
  373. }
  374. } else {
  375. /* non-index objects start out life as files */
  376. if (!next->d_inode) {
  377. ret = cachefiles_has_space(cache, 1, 0);
  378. if (ret < 0)
  379. goto create_error;
  380. start = jiffies;
  381. ret = vfs_create(dir->d_inode, next, S_IFREG, NULL);
  382. cachefiles_hist(cachefiles_create_histogram, start);
  383. if (ret < 0)
  384. goto create_error;
  385. ASSERT(next->d_inode);
  386. _debug("create -> %p{%p{ino=%lu}}",
  387. next, next->d_inode, next->d_inode->i_ino);
  388. } else if (!S_ISDIR(next->d_inode->i_mode) &&
  389. !S_ISREG(next->d_inode->i_mode)
  390. ) {
  391. kerror("inode %lu is not a file or directory",
  392. next->d_inode->i_ino);
  393. ret = -ENOBUFS;
  394. goto error;
  395. }
  396. }
  397. /* process the next component */
  398. if (key) {
  399. _debug("advance");
  400. mutex_unlock(&dir->d_inode->i_mutex);
  401. dput(dir);
  402. dir = next;
  403. next = NULL;
  404. goto advance;
  405. }
  406. /* we've found the object we were looking for */
  407. object->dentry = next;
  408. /* if we've found that the terminal object exists, then we need to
  409. * check its attributes and delete it if it's out of date */
  410. if (!object->new) {
  411. _debug("validate '%*.*s'",
  412. next->d_name.len, next->d_name.len, next->d_name.name);
  413. ret = cachefiles_check_object_xattr(object, auxdata);
  414. if (ret == -ESTALE) {
  415. /* delete the object (the deleter drops the directory
  416. * mutex) */
  417. object->dentry = NULL;
  418. ret = cachefiles_bury_object(cache, dir, next);
  419. dput(next);
  420. next = NULL;
  421. if (ret < 0)
  422. goto delete_error;
  423. _debug("redo lookup");
  424. goto lookup_again;
  425. }
  426. }
  427. /* note that we're now using this object */
  428. ret = cachefiles_mark_object_active(cache, object);
  429. mutex_unlock(&dir->d_inode->i_mutex);
  430. dput(dir);
  431. dir = NULL;
  432. if (ret == -ETIMEDOUT)
  433. goto mark_active_timed_out;
  434. _debug("=== OBTAINED_OBJECT ===");
  435. if (object->new) {
  436. /* attach data to a newly constructed terminal object */
  437. ret = cachefiles_set_object_xattr(object, auxdata);
  438. if (ret < 0)
  439. goto check_error;
  440. } else {
  441. /* always update the atime on an object we've just looked up
  442. * (this is used to keep track of culling, and atimes are only
  443. * updated by read, write and readdir but not lookup or
  444. * open) */
  445. touch_atime(cache->mnt, next);
  446. }
  447. /* open a file interface onto a data file */
  448. if (object->type != FSCACHE_COOKIE_TYPE_INDEX) {
  449. if (S_ISREG(object->dentry->d_inode->i_mode)) {
  450. const struct address_space_operations *aops;
  451. ret = -EPERM;
  452. aops = object->dentry->d_inode->i_mapping->a_ops;
  453. if (!aops->bmap)
  454. goto check_error;
  455. object->backer = object->dentry;
  456. } else {
  457. BUG(); // TODO: open file in data-class subdir
  458. }
  459. }
  460. object->new = 0;
  461. fscache_obtained_object(&object->fscache);
  462. _leave(" = 0 [%lu]", object->dentry->d_inode->i_ino);
  463. return 0;
  464. create_error:
  465. _debug("create error %d", ret);
  466. if (ret == -EIO)
  467. cachefiles_io_error(cache, "Create/mkdir failed");
  468. goto error;
  469. mark_active_timed_out:
  470. _debug("mark active timed out");
  471. goto release_dentry;
  472. check_error:
  473. _debug("check error %d", ret);
  474. write_lock(&cache->active_lock);
  475. rb_erase(&object->active_node, &cache->active_nodes);
  476. clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
  477. wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
  478. write_unlock(&cache->active_lock);
  479. release_dentry:
  480. dput(object->dentry);
  481. object->dentry = NULL;
  482. goto error_out;
  483. delete_error:
  484. _debug("delete error %d", ret);
  485. goto error_out2;
  486. lookup_error:
  487. _debug("lookup error %ld", PTR_ERR(next));
  488. ret = PTR_ERR(next);
  489. if (ret == -EIO)
  490. cachefiles_io_error(cache, "Lookup failed");
  491. next = NULL;
  492. error:
  493. mutex_unlock(&dir->d_inode->i_mutex);
  494. dput(next);
  495. error_out2:
  496. dput(dir);
  497. error_out:
  498. _leave(" = error %d", -ret);
  499. return ret;
  500. }
  501. /*
  502. * get a subdirectory
  503. */
  504. struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  505. struct dentry *dir,
  506. const char *dirname)
  507. {
  508. struct dentry *subdir;
  509. unsigned long start;
  510. int ret;
  511. _enter(",,%s", dirname);
  512. /* search the current directory for the element name */
  513. mutex_lock(&dir->d_inode->i_mutex);
  514. start = jiffies;
  515. subdir = lookup_one_len(dirname, dir, strlen(dirname));
  516. cachefiles_hist(cachefiles_lookup_histogram, start);
  517. if (IS_ERR(subdir)) {
  518. if (PTR_ERR(subdir) == -ENOMEM)
  519. goto nomem_d_alloc;
  520. goto lookup_error;
  521. }
  522. _debug("subdir -> %p %s",
  523. subdir, subdir->d_inode ? "positive" : "negative");
  524. /* we need to create the subdir if it doesn't exist yet */
  525. if (!subdir->d_inode) {
  526. ret = cachefiles_has_space(cache, 1, 0);
  527. if (ret < 0)
  528. goto mkdir_error;
  529. _debug("attempt mkdir");
  530. ret = vfs_mkdir(dir->d_inode, subdir, 0700);
  531. if (ret < 0)
  532. goto mkdir_error;
  533. ASSERT(subdir->d_inode);
  534. _debug("mkdir -> %p{%p{ino=%lu}}",
  535. subdir,
  536. subdir->d_inode,
  537. subdir->d_inode->i_ino);
  538. }
  539. mutex_unlock(&dir->d_inode->i_mutex);
  540. /* we need to make sure the subdir is a directory */
  541. ASSERT(subdir->d_inode);
  542. if (!S_ISDIR(subdir->d_inode->i_mode)) {
  543. kerror("%s is not a directory", dirname);
  544. ret = -EIO;
  545. goto check_error;
  546. }
  547. ret = -EPERM;
  548. if (!subdir->d_inode->i_op ||
  549. !subdir->d_inode->i_op->setxattr ||
  550. !subdir->d_inode->i_op->getxattr ||
  551. !subdir->d_inode->i_op->lookup ||
  552. !subdir->d_inode->i_op->mkdir ||
  553. !subdir->d_inode->i_op->create ||
  554. !subdir->d_inode->i_op->rename ||
  555. !subdir->d_inode->i_op->rmdir ||
  556. !subdir->d_inode->i_op->unlink)
  557. goto check_error;
  558. _leave(" = [%lu]", subdir->d_inode->i_ino);
  559. return subdir;
  560. check_error:
  561. dput(subdir);
  562. _leave(" = %d [check]", ret);
  563. return ERR_PTR(ret);
  564. mkdir_error:
  565. mutex_unlock(&dir->d_inode->i_mutex);
  566. dput(subdir);
  567. kerror("mkdir %s failed with error %d", dirname, ret);
  568. return ERR_PTR(ret);
  569. lookup_error:
  570. mutex_unlock(&dir->d_inode->i_mutex);
  571. ret = PTR_ERR(subdir);
  572. kerror("Lookup %s failed with error %d", dirname, ret);
  573. return ERR_PTR(ret);
  574. nomem_d_alloc:
  575. mutex_unlock(&dir->d_inode->i_mutex);
  576. _leave(" = -ENOMEM");
  577. return ERR_PTR(-ENOMEM);
  578. }
  579. /*
  580. * find out if an object is in use or not
  581. * - if finds object and it's not in use:
  582. * - returns a pointer to the object and a reference on it
  583. * - returns with the directory locked
  584. */
  585. static struct dentry *cachefiles_check_active(struct cachefiles_cache *cache,
  586. struct dentry *dir,
  587. char *filename)
  588. {
  589. struct cachefiles_object *object;
  590. struct rb_node *_n;
  591. struct dentry *victim;
  592. unsigned long start;
  593. int ret;
  594. //_enter(",%*.*s/,%s",
  595. // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
  596. /* look up the victim */
  597. mutex_lock_nested(&dir->d_inode->i_mutex, 1);
  598. start = jiffies;
  599. victim = lookup_one_len(filename, dir, strlen(filename));
  600. cachefiles_hist(cachefiles_lookup_histogram, start);
  601. if (IS_ERR(victim))
  602. goto lookup_error;
  603. //_debug("victim -> %p %s",
  604. // victim, victim->d_inode ? "positive" : "negative");
  605. /* if the object is no longer there then we probably retired the object
  606. * at the netfs's request whilst the cull was in progress
  607. */
  608. if (!victim->d_inode) {
  609. mutex_unlock(&dir->d_inode->i_mutex);
  610. dput(victim);
  611. _leave(" = -ENOENT [absent]");
  612. return ERR_PTR(-ENOENT);
  613. }
  614. /* check to see if we're using this object */
  615. read_lock(&cache->active_lock);
  616. _n = cache->active_nodes.rb_node;
  617. while (_n) {
  618. object = rb_entry(_n, struct cachefiles_object, active_node);
  619. if (object->dentry > victim)
  620. _n = _n->rb_left;
  621. else if (object->dentry < victim)
  622. _n = _n->rb_right;
  623. else
  624. goto object_in_use;
  625. }
  626. read_unlock(&cache->active_lock);
  627. //_leave(" = %p", victim);
  628. return victim;
  629. object_in_use:
  630. read_unlock(&cache->active_lock);
  631. mutex_unlock(&dir->d_inode->i_mutex);
  632. dput(victim);
  633. //_leave(" = -EBUSY [in use]");
  634. return ERR_PTR(-EBUSY);
  635. lookup_error:
  636. mutex_unlock(&dir->d_inode->i_mutex);
  637. ret = PTR_ERR(victim);
  638. if (ret == -ENOENT) {
  639. /* file or dir now absent - probably retired by netfs */
  640. _leave(" = -ESTALE [absent]");
  641. return ERR_PTR(-ESTALE);
  642. }
  643. if (ret == -EIO) {
  644. cachefiles_io_error(cache, "Lookup failed");
  645. } else if (ret != -ENOMEM) {
  646. kerror("Internal error: %d", ret);
  647. ret = -EIO;
  648. }
  649. _leave(" = %d", ret);
  650. return ERR_PTR(ret);
  651. }
  652. /*
  653. * cull an object if it's not in use
  654. * - called only by cache manager daemon
  655. */
  656. int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  657. char *filename)
  658. {
  659. struct dentry *victim;
  660. int ret;
  661. _enter(",%*.*s/,%s",
  662. dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
  663. victim = cachefiles_check_active(cache, dir, filename);
  664. if (IS_ERR(victim))
  665. return PTR_ERR(victim);
  666. _debug("victim -> %p %s",
  667. victim, victim->d_inode ? "positive" : "negative");
  668. /* okay... the victim is not being used so we can cull it
  669. * - start by marking it as stale
  670. */
  671. _debug("victim is cullable");
  672. ret = cachefiles_remove_object_xattr(cache, victim);
  673. if (ret < 0)
  674. goto error_unlock;
  675. /* actually remove the victim (drops the dir mutex) */
  676. _debug("bury");
  677. ret = cachefiles_bury_object(cache, dir, victim);
  678. if (ret < 0)
  679. goto error;
  680. dput(victim);
  681. _leave(" = 0");
  682. return 0;
  683. error_unlock:
  684. mutex_unlock(&dir->d_inode->i_mutex);
  685. error:
  686. dput(victim);
  687. if (ret == -ENOENT) {
  688. /* file or dir now absent - probably retired by netfs */
  689. _leave(" = -ESTALE [absent]");
  690. return -ESTALE;
  691. }
  692. if (ret != -ENOMEM) {
  693. kerror("Internal error: %d", ret);
  694. ret = -EIO;
  695. }
  696. _leave(" = %d", ret);
  697. return ret;
  698. }
  699. /*
  700. * find out if an object is in use or not
  701. * - called only by cache manager daemon
  702. * - returns -EBUSY or 0 to indicate whether an object is in use or not
  703. */
  704. int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir,
  705. char *filename)
  706. {
  707. struct dentry *victim;
  708. //_enter(",%*.*s/,%s",
  709. // dir->d_name.len, dir->d_name.len, dir->d_name.name, filename);
  710. victim = cachefiles_check_active(cache, dir, filename);
  711. if (IS_ERR(victim))
  712. return PTR_ERR(victim);
  713. mutex_unlock(&dir->d_inode->i_mutex);
  714. dput(victim);
  715. //_leave(" = 0");
  716. return 0;
  717. }