namei.c 24 KB

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