lookup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /* but, allow access to all media files in adoptable and internal storage */
  153. if (__kuid_val(lower_inode->i_uid) != AID_MEDIA_RW && __kgid_val(lower_inode->i_gid) != AID_MEDIA_RW) {
  154. ret_dentry = ERR_PTR(-EXDEV);
  155. goto out;
  156. }
  157. }
  158. /*
  159. * We allocate our new inode below by calling sdcardfs_iget,
  160. * which will initialize some of the new inode's fields
  161. */
  162. /* inherit lower inode number for sdcardfs's inode */
  163. inode = sdcardfs_iget(sb, lower_inode, id);
  164. if (IS_ERR(inode)) {
  165. ret_dentry = ERR_CAST(inode);
  166. goto out;
  167. }
  168. ret_dentry = d_splice_alias(inode, dentry);
  169. dentry = ret_dentry ?: dentry;
  170. if (!IS_ERR(dentry))
  171. update_derived_permission_lock(dentry);
  172. out:
  173. return ret_dentry;
  174. }
  175. /*
  176. * Connect an sdcardfs inode dentry/inode with several lower ones. This is
  177. * the classic stackable file system "vnode interposition" action.
  178. *
  179. * @dentry: sdcardfs's dentry which interposes on lower one
  180. * @sb: sdcardfs's super_block
  181. * @lower_path: the lower path (caller does path_get/put)
  182. */
  183. int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
  184. struct path *lower_path, userid_t id)
  185. {
  186. struct dentry *ret_dentry;
  187. ret_dentry = __sdcardfs_interpose(dentry, sb, lower_path, id);
  188. return PTR_ERR(ret_dentry);
  189. }
  190. /*
  191. * Main driver function for sdcardfs's lookup.
  192. *
  193. * Returns: NULL (ok), ERR_PTR if an error occurred.
  194. * Fills in lower_parent_path with <dentry,mnt> on success.
  195. */
  196. static struct dentry *__sdcardfs_lookup(struct dentry *dentry,
  197. struct nameidata *nd, struct path *lower_parent_path, userid_t id)
  198. {
  199. int err = 0;
  200. struct vfsmount *lower_dir_mnt;
  201. struct dentry *lower_dir_dentry = NULL;
  202. struct dentry *lower_dentry;
  203. const struct qstr *name;
  204. struct nameidata lower_nd;
  205. struct qstr dname;
  206. struct dentry *ret_dentry = NULL;
  207. struct sdcardfs_sb_info *sbi;
  208. sbi = SDCARDFS_SB(dentry->d_sb);
  209. /* must initialize dentry operations */
  210. d_set_d_op(dentry, &sdcardfs_ci_dops);
  211. if (IS_ROOT(dentry))
  212. goto out;
  213. name = &dentry->d_name;
  214. /* now start the actual lookup procedure */
  215. lower_dir_dentry = lower_parent_path->dentry;
  216. lower_dir_mnt = lower_parent_path->mnt;
  217. /* Use vfs_path_lookup to check if the dentry exists or not */
  218. err = vfs_path_lookup(lower_dir_dentry, lower_dir_mnt, name->name, 0,
  219. &lower_nd.path);
  220. /* check for other cases */
  221. if (err == -ENOENT) {
  222. struct dentry *child;
  223. struct dentry *match = NULL;
  224. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  225. spin_lock(&lower_dir_dentry->d_lock);
  226. list_for_each_entry(child, &lower_dir_dentry->d_subdirs, d_child) {
  227. if (child && child->d_inode) {
  228. if (qstr_case_eq(&child->d_name, name)) {
  229. match = dget(child);
  230. break;
  231. }
  232. }
  233. }
  234. spin_unlock(&lower_dir_dentry->d_lock);
  235. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  236. if (match) {
  237. err = vfs_path_lookup(lower_dir_dentry,
  238. lower_dir_mnt,
  239. match->d_name.name, 0,
  240. &lower_nd.path);
  241. dput(match);
  242. }
  243. }
  244. /* no error: handle positive dentries */
  245. if (!err) {
  246. /* check if the dentry is an obb dentry
  247. * if true, the lower_inode must be replaced with
  248. * the inode of the graft path
  249. */
  250. if (need_graft_path(dentry)) {
  251. /* setup_obb_dentry()
  252. * The lower_path will be stored to the dentry's orig_path
  253. * and the base obbpath will be copyed to the lower_path variable.
  254. * if an error returned, there's no change in the lower_path
  255. * returns: -ERRNO if error (0: no error)
  256. */
  257. err = setup_obb_dentry(dentry, &lower_nd.path);
  258. if (err) {
  259. /* if the sbi->obbpath is not available, we can optionally
  260. * setup the lower_path with its orig_path.
  261. * but, the current implementation just returns an error
  262. * because the sdcard daemon also regards this case as
  263. * a lookup fail.
  264. */
  265. pr_info("sdcardfs: base obbpath is not available\n");
  266. sdcardfs_put_reset_orig_path(dentry);
  267. goto out;
  268. }
  269. }
  270. sdcardfs_set_lower_path(dentry, &lower_nd.path);
  271. ret_dentry =
  272. __sdcardfs_interpose(dentry, dentry->d_sb, &lower_nd.path, id);
  273. if (IS_ERR(ret_dentry)) {
  274. err = PTR_ERR(ret_dentry);
  275. /* path_put underlying path on error */
  276. sdcardfs_put_reset_lower_path(dentry);
  277. }
  278. goto out;
  279. }
  280. /*
  281. * We don't consider ENOENT an error, and we want to return a
  282. * negative dentry.
  283. */
  284. if (err && err != -ENOENT)
  285. goto out;
  286. /* instatiate a new negative dentry */
  287. dname.name = name->name;
  288. dname.len = name->len;
  289. /* See if the low-level filesystem might want
  290. * to use its own hash
  291. */
  292. lower_dentry = d_hash_and_lookup(lower_dir_dentry, &dname);
  293. if (IS_ERR(lower_dentry))
  294. return lower_dentry;
  295. if (!lower_dentry) {
  296. /* We called vfs_path_lookup earlier, and did not get a negative
  297. * dentry then. Don't confuse the lower filesystem by forcing one
  298. * on it now...
  299. */
  300. err = -ENOENT;
  301. goto out;
  302. }
  303. lower_nd.path.dentry = lower_dentry;
  304. lower_nd.path.mnt = mntget(lower_dir_mnt);
  305. sdcardfs_set_lower_path(dentry, &lower_nd.path);
  306. /*
  307. * If the intent is to create a file, then don't return an error, so
  308. * the VFS will continue the process of making this negative dentry
  309. * into a positive one.
  310. */
  311. if (nd) {
  312. if (nd->flags & (LOOKUP_CREATE|LOOKUP_RENAME_TARGET))
  313. err = 0;
  314. } else
  315. err = 0;
  316. out:
  317. if (err)
  318. return ERR_PTR(err);
  319. return ret_dentry;
  320. }
  321. /*
  322. * On success:
  323. * fills dentry object appropriate values and returns NULL.
  324. * On fail (== error)
  325. * returns error ptr
  326. *
  327. * @dir : Parent inode. It is locked (dir->i_mutex)
  328. * @dentry : Target dentry to lookup. we should set each of fields.
  329. * (dentry->d_name is initialized already)
  330. * @nd : nameidata of parent inode
  331. */
  332. struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
  333. struct nameidata *nd)
  334. {
  335. struct dentry *ret = NULL, *parent;
  336. struct path lower_parent_path;
  337. int err = 0;
  338. const struct cred *saved_cred = NULL;
  339. parent = dget_parent(dentry);
  340. if (!check_caller_access_to_name(parent->d_inode, &dentry->d_name)) {
  341. ret = ERR_PTR(-EACCES);
  342. goto out_err;
  343. }
  344. /* save current_cred and override it */
  345. saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
  346. SDCARDFS_I(dir)->data);
  347. if (!saved_cred) {
  348. ret = ERR_PTR(-ENOMEM);
  349. goto out_err;
  350. }
  351. sdcardfs_get_lower_path(parent, &lower_parent_path);
  352. /* allocate dentry private data. We free it in ->d_release */
  353. err = new_dentry_private_data(dentry);
  354. if (err) {
  355. ret = ERR_PTR(err);
  356. goto out;
  357. }
  358. ret = __sdcardfs_lookup(dentry, nd, &lower_parent_path,
  359. SDCARDFS_I(dir)->data->userid);
  360. if (IS_ERR(ret))
  361. goto out;
  362. if (ret)
  363. dentry = ret;
  364. if (dentry->d_inode) {
  365. fsstack_copy_attr_times(dentry->d_inode,
  366. sdcardfs_lower_inode(dentry->d_inode));
  367. /* get derived permission */
  368. get_derived_permission(parent, dentry);
  369. fixup_tmp_permissions(dentry->d_inode);
  370. fixup_lower_ownership(dentry, dentry->d_name.name);
  371. }
  372. /* update parent directory's atime */
  373. fsstack_copy_attr_atime(parent->d_inode,
  374. sdcardfs_lower_inode(parent->d_inode));
  375. out:
  376. sdcardfs_put_lower_path(parent, &lower_parent_path);
  377. revert_fsids(saved_cred);
  378. out_err:
  379. dput(parent);
  380. return ret;
  381. }