namei.c 24 KB

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