namei.c 25 KB

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