lookup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * fs/sdcardfs/lookup.c
  3. *
  4. * Copyright (c) 2013 Samsung Electronics Co. Ltd
  5. * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
  6. * Sunghwan Yun, Sungjong Seo
  7. *
  8. * This program has been developed as a stackable file system based on
  9. * the WrapFS which written by
  10. *
  11. * Copyright (c) 1998-2011 Erez Zadok
  12. * Copyright (c) 2009 Shrikar Archak
  13. * Copyright (c) 2003-2011 Stony Brook University
  14. * Copyright (c) 2003-2011 The Research Foundation of SUNY
  15. *
  16. * This file is dual licensed. It may be redistributed and/or modified
  17. * under the terms of the Apache 2.0 License OR version 2 of the GNU
  18. * General Public License.
  19. */
  20. #include "sdcardfs.h"
  21. #include "linux/delay.h"
  22. /* The dentry cache is just so we have properly sized dentries */
  23. static struct kmem_cache *sdcardfs_dentry_cachep;
  24. int sdcardfs_init_dentry_cache(void)
  25. {
  26. sdcardfs_dentry_cachep =
  27. kmem_cache_create("sdcardfs_dentry",
  28. sizeof(struct sdcardfs_dentry_info),
  29. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  30. return sdcardfs_dentry_cachep ? 0 : -ENOMEM;
  31. }
  32. void sdcardfs_destroy_dentry_cache(void)
  33. {
  34. kmem_cache_destroy(sdcardfs_dentry_cachep);
  35. }
  36. void free_dentry_private_data(struct dentry *dentry)
  37. {
  38. kmem_cache_free(sdcardfs_dentry_cachep, dentry->d_fsdata);
  39. dentry->d_fsdata = NULL;
  40. }
  41. /* allocate new dentry private data */
  42. int new_dentry_private_data(struct dentry *dentry)
  43. {
  44. struct sdcardfs_dentry_info *info = SDCARDFS_D(dentry);
  45. /* use zalloc to init dentry_info.lower_path */
  46. info = kmem_cache_zalloc(sdcardfs_dentry_cachep, GFP_ATOMIC);
  47. if (!info)
  48. return -ENOMEM;
  49. spin_lock_init(&info->lock);
  50. dentry->d_fsdata = info;
  51. return 0;
  52. }
  53. struct inode_data {
  54. struct inode *lower_inode;
  55. userid_t id;
  56. };
  57. static int sdcardfs_inode_test(struct inode *inode, void *candidate_data/*void *candidate_lower_inode*/)
  58. {
  59. struct inode *current_lower_inode = sdcardfs_lower_inode(inode);
  60. userid_t current_userid = SDCARDFS_I(inode)->data->userid;
  61. if (current_lower_inode == ((struct inode_data *)candidate_data)->lower_inode &&
  62. current_userid == ((struct inode_data *)candidate_data)->id)
  63. return 1; /* found a match */
  64. else
  65. return 0; /* no match */
  66. }
  67. static int sdcardfs_inode_set(struct inode *inode, void *lower_inode)
  68. {
  69. /* we do actual inode initialization in sdcardfs_iget */
  70. return 0;
  71. }
  72. struct inode *sdcardfs_iget(struct super_block *sb, struct inode *lower_inode, userid_t id)
  73. {
  74. struct sdcardfs_inode_info *info;
  75. struct inode_data data;
  76. struct inode *inode; /* the new inode to return */
  77. if (!igrab(lower_inode))
  78. return ERR_PTR(-ESTALE);
  79. data.id = id;
  80. data.lower_inode = lower_inode;
  81. inode = iget5_locked(sb, /* our superblock */
  82. /*
  83. * hashval: we use inode number, but we can
  84. * also use "(unsigned long)lower_inode"
  85. * instead.
  86. */
  87. lower_inode->i_ino, /* hashval */
  88. sdcardfs_inode_test, /* inode comparison function */
  89. sdcardfs_inode_set, /* inode init function */
  90. &data); /* data passed to test+set fxns */
  91. if (!inode) {
  92. iput(lower_inode);
  93. return ERR_PTR(-ENOMEM);
  94. }
  95. /* if found a cached inode, then just return it */
  96. if (!(inode->i_state & I_NEW)) {
  97. iput(lower_inode);
  98. return inode;
  99. }
  100. /* initialize new inode */
  101. info = SDCARDFS_I(inode);
  102. inode->i_ino = lower_inode->i_ino;
  103. sdcardfs_set_lower_inode(inode, lower_inode);
  104. inode->i_version++;
  105. /* use different set of inode ops for symlinks & directories */
  106. if (S_ISDIR(lower_inode->i_mode))
  107. inode->i_op = &sdcardfs_dir_iops;
  108. else if (S_ISLNK(lower_inode->i_mode))
  109. inode->i_op = &sdcardfs_symlink_iops;
  110. else
  111. inode->i_op = &sdcardfs_main_iops;
  112. /* use different set of file ops for directories */
  113. if (S_ISDIR(lower_inode->i_mode))
  114. inode->i_fop = &sdcardfs_dir_fops;
  115. else
  116. inode->i_fop = &sdcardfs_main_fops;
  117. inode->i_mapping->a_ops = &sdcardfs_aops;
  118. inode->i_atime.tv_sec = 0;
  119. inode->i_atime.tv_nsec = 0;
  120. inode->i_mtime.tv_sec = 0;
  121. inode->i_mtime.tv_nsec = 0;
  122. inode->i_ctime.tv_sec = 0;
  123. inode->i_ctime.tv_nsec = 0;
  124. /* properly initialize special inodes */
  125. if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
  126. S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
  127. init_special_inode(inode, lower_inode->i_mode,
  128. lower_inode->i_rdev);
  129. /* all well, copy inode attributes */
  130. sdcardfs_copy_and_fix_attrs(inode, lower_inode);
  131. fsstack_copy_inode_size(inode, lower_inode);
  132. unlock_new_inode(inode);
  133. return inode;
  134. }
  135. /*
  136. * Helper interpose routine, called directly by ->lookup to handle
  137. * spliced dentries.
  138. */
  139. static struct dentry *__sdcardfs_interpose(struct dentry *dentry,
  140. struct super_block *sb,
  141. struct path *lower_path,
  142. userid_t id)
  143. {
  144. struct inode *inode;
  145. struct inode *lower_inode;
  146. struct super_block *lower_sb;
  147. struct dentry *ret_dentry;
  148. lower_inode = lower_path->dentry->d_inode;
  149. lower_sb = sdcardfs_lower_super(sb);
  150. /* check that the lower file system didn't cross a mount point */
  151. if (lower_inode->i_sb != lower_sb) {
  152. ret_dentry = ERR_PTR(-EXDEV);
  153. goto out;
  154. }
  155. /*
  156. * We allocate our new inode below by calling sdcardfs_iget,
  157. * which will initialize some of the new inode's fields
  158. */
  159. /* inherit lower inode number for sdcardfs's inode */
  160. inode = sdcardfs_iget(sb, lower_inode, id);
  161. if (IS_ERR(inode)) {
  162. ret_dentry = ERR_CAST(inode);
  163. goto out;
  164. }
  165. ret_dentry = d_splice_alias(inode, dentry);
  166. dentry = ret_dentry ?: dentry;
  167. if (!IS_ERR(dentry))
  168. update_derived_permission_lock(dentry);
  169. out:
  170. return ret_dentry;
  171. }
  172. /*
  173. * Connect an sdcardfs inode dentry/inode with several lower ones. This is
  174. * the classic stackable file system "vnode interposition" action.
  175. *
  176. * @dentry: sdcardfs's dentry which interposes on lower one
  177. * @sb: sdcardfs's super_block
  178. * @lower_path: the lower path (caller does path_get/put)
  179. */
  180. int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
  181. struct path *lower_path, userid_t id)
  182. {
  183. struct dentry *ret_dentry;
  184. ret_dentry = __sdcardfs_interpose(dentry, sb, lower_path, id);
  185. return PTR_ERR(ret_dentry);
  186. }
  187. /*
  188. * Main driver function for sdcardfs's lookup.
  189. *
  190. * Returns: NULL (ok), ERR_PTR if an error occurred.
  191. * Fills in lower_parent_path with <dentry,mnt> on success.
  192. */
  193. static struct dentry *__sdcardfs_lookup(struct dentry *dentry,
  194. struct nameidata *nd, struct path *lower_parent_path, userid_t id)
  195. {
  196. int err = 0;
  197. struct vfsmount *lower_dir_mnt;
  198. struct dentry *lower_dir_dentry = NULL;
  199. struct dentry *lower_dentry;
  200. const struct qstr *name;
  201. struct nameidata lower_nd;
  202. struct qstr dname;
  203. struct dentry *ret_dentry = NULL;
  204. struct sdcardfs_sb_info *sbi;
  205. sbi = SDCARDFS_SB(dentry->d_sb);
  206. /* must initialize dentry operations */
  207. d_set_d_op(dentry, &sdcardfs_ci_dops);
  208. if (IS_ROOT(dentry))
  209. goto out;
  210. name = &dentry->d_name;
  211. /* now start the actual lookup procedure */
  212. lower_dir_dentry = lower_parent_path->dentry;
  213. lower_dir_mnt = lower_parent_path->mnt;
  214. /* Use vfs_path_lookup to check if the dentry exists or not */
  215. err = vfs_path_lookup(lower_dir_dentry, lower_dir_mnt, name->name, 0,
  216. &lower_nd.path);
  217. /* check for other cases */
  218. if (err == -ENOENT) {
  219. struct dentry *child;
  220. struct dentry *match = NULL;
  221. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  222. spin_lock(&lower_dir_dentry->d_lock);
  223. list_for_each_entry(child, &lower_dir_dentry->d_subdirs, d_child) {
  224. if (child && child->d_inode) {
  225. if (qstr_case_eq(&child->d_name, name)) {
  226. match = dget(child);
  227. break;
  228. }
  229. }
  230. }
  231. spin_unlock(&lower_dir_dentry->d_lock);
  232. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  233. if (match) {
  234. err = vfs_path_lookup(lower_dir_dentry,
  235. lower_dir_mnt,
  236. match->d_name.name, 0,
  237. &lower_nd.path);
  238. dput(match);
  239. }
  240. }
  241. /* no error: handle positive dentries */
  242. if (!err) {
  243. /* check if the dentry is an obb dentry
  244. * if true, the lower_inode must be replaced with
  245. * the inode of the graft path
  246. */
  247. if (need_graft_path(dentry)) {
  248. /* setup_obb_dentry()
  249. * The lower_path will be stored to the dentry's orig_path
  250. * and the base obbpath will be copyed to the lower_path variable.
  251. * if an error returned, there's no change in the lower_path
  252. * returns: -ERRNO if error (0: no error)
  253. */
  254. err = setup_obb_dentry(dentry, &lower_nd.path);
  255. if (err) {
  256. /* if the sbi->obbpath is not available, we can optionally
  257. * setup the lower_path with its orig_path.
  258. * but, the current implementation just returns an error
  259. * because the sdcard daemon also regards this case as
  260. * a lookup fail.
  261. */
  262. pr_info("sdcardfs: base obbpath is not available\n");
  263. sdcardfs_put_reset_orig_path(dentry);
  264. goto out;
  265. }
  266. }
  267. sdcardfs_set_lower_path(dentry, &lower_nd.path);
  268. ret_dentry =
  269. __sdcardfs_interpose(dentry, dentry->d_sb, &lower_nd.path, id);
  270. if (IS_ERR(ret_dentry)) {
  271. err = PTR_ERR(ret_dentry);
  272. /* path_put underlying path on error */
  273. sdcardfs_put_reset_lower_path(dentry);
  274. }
  275. goto out;
  276. }
  277. /*
  278. * We don't consider ENOENT an error, and we want to return a
  279. * negative dentry.
  280. */
  281. if (err && err != -ENOENT)
  282. goto out;
  283. /* instatiate a new negative dentry */
  284. dname.name = name->name;
  285. dname.len = name->len;
  286. /* See if the low-level filesystem might want
  287. * to use its own hash
  288. */
  289. lower_dentry = d_hash_and_lookup(lower_dir_dentry, &dname);
  290. if (IS_ERR(lower_dentry))
  291. return lower_dentry;
  292. if (!lower_dentry) {
  293. /* We called vfs_path_lookup earlier, and did not get a negative
  294. * dentry then. Don't confuse the lower filesystem by forcing one
  295. * on it now...
  296. */
  297. err = -ENOENT;
  298. goto out;
  299. }
  300. lower_nd.path.dentry = lower_dentry;
  301. lower_nd.path.mnt = mntget(lower_dir_mnt);
  302. sdcardfs_set_lower_path(dentry, &lower_nd.path);
  303. /*
  304. * If the intent is to create a file, then don't return an error, so
  305. * the VFS will continue the process of making this negative dentry
  306. * into a positive one.
  307. */
  308. if (nd) {
  309. if (nd->flags & (LOOKUP_CREATE|LOOKUP_RENAME_TARGET))
  310. err = 0;
  311. } else
  312. err = 0;
  313. out:
  314. if (err)
  315. return ERR_PTR(err);
  316. return ret_dentry;
  317. }
  318. /*
  319. * On success:
  320. * fills dentry object appropriate values and returns NULL.
  321. * On fail (== error)
  322. * returns error ptr
  323. *
  324. * @dir : Parent inode. It is locked (dir->i_mutex)
  325. * @dentry : Target dentry to lookup. we should set each of fields.
  326. * (dentry->d_name is initialized already)
  327. * @nd : nameidata of parent inode
  328. */
  329. struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
  330. struct nameidata *nd)
  331. {
  332. struct dentry *ret = NULL, *parent;
  333. struct path lower_parent_path;
  334. int err = 0;
  335. const struct cred *saved_cred = NULL;
  336. parent = dget_parent(dentry);
  337. if (!check_caller_access_to_name(parent->d_inode, &dentry->d_name)) {
  338. ret = ERR_PTR(-EACCES);
  339. goto out_err;
  340. }
  341. /* save current_cred and override it */
  342. saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
  343. SDCARDFS_I(dir)->data);
  344. if (!saved_cred) {
  345. ret = ERR_PTR(-ENOMEM);
  346. goto out_err;
  347. }
  348. sdcardfs_get_lower_path(parent, &lower_parent_path);
  349. /* allocate dentry private data. We free it in ->d_release */
  350. err = new_dentry_private_data(dentry);
  351. if (err) {
  352. ret = ERR_PTR(err);
  353. goto out;
  354. }
  355. ret = __sdcardfs_lookup(dentry, nd, &lower_parent_path,
  356. SDCARDFS_I(dir)->data->userid);
  357. if (IS_ERR(ret))
  358. goto out;
  359. if (ret)
  360. dentry = ret;
  361. if (dentry->d_inode) {
  362. fsstack_copy_attr_times(dentry->d_inode,
  363. sdcardfs_lower_inode(dentry->d_inode));
  364. /* get derived permission */
  365. get_derived_permission(parent, dentry);
  366. fixup_tmp_permissions(dentry->d_inode);
  367. fixup_lower_ownership(dentry, dentry->d_name.name);
  368. }
  369. /* update parent directory's atime */
  370. fsstack_copy_attr_atime(parent->d_inode,
  371. sdcardfs_lower_inode(parent->d_inode));
  372. out:
  373. sdcardfs_put_lower_path(parent, &lower_parent_path);
  374. revert_fsids(saved_cred);
  375. out_err:
  376. dput(parent);
  377. return ret;
  378. }