expfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #include <linux/exportfs.h>
  2. #include <linux/fs.h>
  3. #include <linux/file.h>
  4. #include <linux/module.h>
  5. #include <linux/mount.h>
  6. #include <linux/namei.h>
  7. struct export_operations export_op_default;
  8. #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
  9. #define dprintk(fmt, args...) do{}while(0)
  10. static struct dentry *
  11. find_acceptable_alias(struct dentry *result,
  12. int (*acceptable)(void *context, struct dentry *dentry),
  13. void *context)
  14. {
  15. struct dentry *dentry, *toput = NULL;
  16. spin_lock(&dcache_lock);
  17. list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
  18. dget_locked(dentry);
  19. spin_unlock(&dcache_lock);
  20. if (toput)
  21. dput(toput);
  22. if (dentry != result && acceptable(context, dentry)) {
  23. dput(result);
  24. return dentry;
  25. }
  26. spin_lock(&dcache_lock);
  27. toput = dentry;
  28. }
  29. spin_unlock(&dcache_lock);
  30. if (toput)
  31. dput(toput);
  32. return NULL;
  33. }
  34. /**
  35. * find_exported_dentry - helper routine to implement export_operations->decode_fh
  36. * @sb: The &super_block identifying the filesystem
  37. * @obj: An opaque identifier of the object to be found - passed to
  38. * get_inode
  39. * @parent: An optional opqaue identifier of the parent of the object.
  40. * @acceptable: A function used to test possible &dentries to see if they are
  41. * acceptable
  42. * @context: A parameter to @acceptable so that it knows on what basis to
  43. * judge.
  44. *
  45. * find_exported_dentry is the central helper routine to enable file systems
  46. * to provide the decode_fh() export_operation. It's main task is to take
  47. * an &inode, find or create an appropriate &dentry structure, and possibly
  48. * splice this into the dcache in the correct place.
  49. *
  50. * The decode_fh() operation provided by the filesystem should call
  51. * find_exported_dentry() with the same parameters that it received except
  52. * that instead of the file handle fragment, pointers to opaque identifiers
  53. * for the object and optionally its parent are passed. The default decode_fh
  54. * routine passes one pointer to the start of the filehandle fragment, and
  55. * one 8 bytes into the fragment. It is expected that most filesystems will
  56. * take this approach, though the offset to the parent identifier may well be
  57. * different.
  58. *
  59. * find_exported_dentry() will call get_dentry to get an dentry pointer from
  60. * the file system. If any &dentry in the d_alias list is acceptable, it will
  61. * be returned. Otherwise find_exported_dentry() will attempt to splice a new
  62. * &dentry into the dcache using get_name() and get_parent() to find the
  63. * appropriate place.
  64. */
  65. struct dentry *
  66. find_exported_dentry(struct super_block *sb, void *obj, void *parent,
  67. int (*acceptable)(void *context, struct dentry *de),
  68. void *context)
  69. {
  70. struct dentry *result = NULL;
  71. struct dentry *target_dir;
  72. int err;
  73. struct export_operations *nops = sb->s_export_op;
  74. struct dentry *alias;
  75. int noprogress;
  76. char nbuf[NAME_MAX+1];
  77. /*
  78. * Attempt to find the inode.
  79. */
  80. result = CALL(sb->s_export_op,get_dentry)(sb,obj);
  81. err = -ESTALE;
  82. if (result == NULL)
  83. goto err_out;
  84. if (IS_ERR(result)) {
  85. err = PTR_ERR(result);
  86. goto err_out;
  87. }
  88. if (S_ISDIR(result->d_inode->i_mode) &&
  89. (result->d_flags & DCACHE_DISCONNECTED)) {
  90. /* it is an unconnected directory, we must connect it */
  91. ;
  92. } else {
  93. if (acceptable(context, result))
  94. return result;
  95. if (S_ISDIR(result->d_inode->i_mode)) {
  96. err = -EACCES;
  97. goto err_result;
  98. }
  99. alias = find_acceptable_alias(result, acceptable, context);
  100. if (alias)
  101. return alias;
  102. }
  103. /* It's a directory, or we are required to confirm the file's
  104. * location in the tree based on the parent information
  105. */
  106. dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb->s_id,*(int*)obj);
  107. if (S_ISDIR(result->d_inode->i_mode))
  108. target_dir = dget(result);
  109. else {
  110. if (parent == NULL)
  111. goto err_result;
  112. target_dir = CALL(sb->s_export_op,get_dentry)(sb,parent);
  113. if (IS_ERR(target_dir))
  114. err = PTR_ERR(target_dir);
  115. if (target_dir == NULL || IS_ERR(target_dir))
  116. goto err_result;
  117. }
  118. /*
  119. * Now we need to make sure that target_dir is properly connected.
  120. * It may already be, as the flag isn't always updated when connection
  121. * happens.
  122. * So, we walk up parent links until we find a connected directory,
  123. * or we run out of directories. Then we find the parent, find
  124. * the name of the child in that parent, and do a lookup.
  125. * This should connect the child into the parent
  126. * We then repeat.
  127. */
  128. /* it is possible that a confused file system might not let us complete
  129. * the path to the root. For example, if get_parent returns a directory
  130. * in which we cannot find a name for the child. While this implies a
  131. * very sick filesystem we don't want it to cause knfsd to spin. Hence
  132. * the noprogress counter. If we go through the loop 10 times (2 is
  133. * probably enough) without getting anywhere, we just give up
  134. */
  135. noprogress= 0;
  136. while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
  137. struct dentry *pd = target_dir;
  138. dget(pd);
  139. spin_lock(&pd->d_lock);
  140. while (!IS_ROOT(pd) &&
  141. (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
  142. struct dentry *parent = pd->d_parent;
  143. dget(parent);
  144. spin_unlock(&pd->d_lock);
  145. dput(pd);
  146. pd = parent;
  147. spin_lock(&pd->d_lock);
  148. }
  149. spin_unlock(&pd->d_lock);
  150. if (!IS_ROOT(pd)) {
  151. /* must have found a connected parent - great */
  152. spin_lock(&pd->d_lock);
  153. pd->d_flags &= ~DCACHE_DISCONNECTED;
  154. spin_unlock(&pd->d_lock);
  155. noprogress = 0;
  156. } else if (pd == sb->s_root) {
  157. printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
  158. spin_lock(&pd->d_lock);
  159. pd->d_flags &= ~DCACHE_DISCONNECTED;
  160. spin_unlock(&pd->d_lock);
  161. noprogress = 0;
  162. } else {
  163. /* we have hit the top of a disconnected path. Try
  164. * to find parent and connect
  165. * note: racing with some other process renaming a
  166. * directory isn't much of a problem here. If someone
  167. * renames the directory, it will end up properly
  168. * connected, which is what we want
  169. */
  170. struct dentry *ppd;
  171. struct dentry *npd;
  172. mutex_lock(&pd->d_inode->i_mutex);
  173. ppd = CALL(nops,get_parent)(pd);
  174. mutex_unlock(&pd->d_inode->i_mutex);
  175. if (IS_ERR(ppd)) {
  176. err = PTR_ERR(ppd);
  177. dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
  178. pd->d_inode->i_ino, err);
  179. dput(pd);
  180. break;
  181. }
  182. dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
  183. err = CALL(nops,get_name)(ppd, nbuf, pd);
  184. if (err) {
  185. dput(ppd);
  186. dput(pd);
  187. if (err == -ENOENT)
  188. /* some race between get_parent and
  189. * get_name? just try again
  190. */
  191. continue;
  192. break;
  193. }
  194. dprintk("find_exported_dentry: found name: %s\n", nbuf);
  195. mutex_lock(&ppd->d_inode->i_mutex);
  196. npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
  197. mutex_unlock(&ppd->d_inode->i_mutex);
  198. if (IS_ERR(npd)) {
  199. err = PTR_ERR(npd);
  200. dprintk("find_exported_dentry: lookup failed: %d\n", err);
  201. dput(ppd);
  202. dput(pd);
  203. break;
  204. }
  205. /* we didn't really want npd, we really wanted
  206. * a side-effect of the lookup.
  207. * hopefully, npd == pd, though it isn't really
  208. * a problem if it isn't
  209. */
  210. if (npd == pd)
  211. noprogress = 0;
  212. else
  213. printk("find_exported_dentry: npd != pd\n");
  214. dput(npd);
  215. dput(ppd);
  216. if (IS_ROOT(pd)) {
  217. /* something went wrong, we have to give up */
  218. dput(pd);
  219. break;
  220. }
  221. }
  222. dput(pd);
  223. }
  224. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  225. /* something went wrong - oh-well */
  226. if (!err)
  227. err = -ESTALE;
  228. goto err_target;
  229. }
  230. /* if we weren't after a directory, have one more step to go */
  231. if (result != target_dir) {
  232. struct dentry *nresult;
  233. err = CALL(nops,get_name)(target_dir, nbuf, result);
  234. if (!err) {
  235. mutex_lock(&target_dir->d_inode->i_mutex);
  236. nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
  237. mutex_unlock(&target_dir->d_inode->i_mutex);
  238. if (!IS_ERR(nresult)) {
  239. if (nresult->d_inode) {
  240. dput(result);
  241. result = nresult;
  242. } else
  243. dput(nresult);
  244. }
  245. }
  246. }
  247. dput(target_dir);
  248. /* now result is properly connected, it is our best bet */
  249. if (acceptable(context, result))
  250. return result;
  251. alias = find_acceptable_alias(result, acceptable, context);
  252. if (alias)
  253. return alias;
  254. /* drat - I just cannot find anything acceptable */
  255. dput(result);
  256. /* It might be justifiable to return ESTALE here,
  257. * but the filehandle at-least looks reasonable good
  258. * and it just be a permission problem, so returning
  259. * -EACCESS is safer
  260. */
  261. return ERR_PTR(-EACCES);
  262. err_target:
  263. dput(target_dir);
  264. err_result:
  265. dput(result);
  266. err_out:
  267. return ERR_PTR(err);
  268. }
  269. static struct dentry *get_parent(struct dentry *child)
  270. {
  271. /* get_parent cannot be supported generically, the locking
  272. * is too icky.
  273. * instead, we just return EACCES. If server reboots or inodes
  274. * get flushed, you lose
  275. */
  276. return ERR_PTR(-EACCES);
  277. }
  278. struct getdents_callback {
  279. char *name; /* name that was found. It already points to a
  280. buffer NAME_MAX+1 is size */
  281. unsigned long ino; /* the inum we are looking for */
  282. int found; /* inode matched? */
  283. int sequence; /* sequence counter */
  284. };
  285. /*
  286. * A rather strange filldir function to capture
  287. * the name matching the specified inode number.
  288. */
  289. static int filldir_one(void * __buf, const char * name, int len,
  290. loff_t pos, u64 ino, unsigned int d_type)
  291. {
  292. struct getdents_callback *buf = __buf;
  293. int result = 0;
  294. buf->sequence++;
  295. if (buf->ino == ino) {
  296. memcpy(buf->name, name, len);
  297. buf->name[len] = '\0';
  298. buf->found = 1;
  299. result = -1;
  300. }
  301. return result;
  302. }
  303. /**
  304. * get_name - default export_operations->get_name function
  305. * @dentry: the directory in which to find a name
  306. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  307. * @child: the dentry for the child directory.
  308. *
  309. * calls readdir on the parent until it finds an entry with
  310. * the same inode number as the child, and returns that.
  311. */
  312. static int get_name(struct dentry *dentry, char *name,
  313. struct dentry *child)
  314. {
  315. struct inode *dir = dentry->d_inode;
  316. int error;
  317. struct file *file;
  318. struct getdents_callback buffer;
  319. error = -ENOTDIR;
  320. if (!dir || !S_ISDIR(dir->i_mode))
  321. goto out;
  322. error = -EINVAL;
  323. if (!dir->i_fop)
  324. goto out;
  325. /*
  326. * Open the directory ...
  327. */
  328. file = dentry_open(dget(dentry), NULL, O_RDONLY);
  329. error = PTR_ERR(file);
  330. if (IS_ERR(file))
  331. goto out;
  332. error = -EINVAL;
  333. if (!file->f_op->readdir)
  334. goto out_close;
  335. buffer.name = name;
  336. buffer.ino = child->d_inode->i_ino;
  337. buffer.found = 0;
  338. buffer.sequence = 0;
  339. while (1) {
  340. int old_seq = buffer.sequence;
  341. error = vfs_readdir(file, filldir_one, &buffer);
  342. if (error < 0)
  343. break;
  344. error = 0;
  345. if (buffer.found)
  346. break;
  347. error = -ENOENT;
  348. if (old_seq == buffer.sequence)
  349. break;
  350. }
  351. out_close:
  352. fput(file);
  353. out:
  354. return error;
  355. }
  356. static struct dentry *get_dentry(struct super_block *sb, void *vobjp)
  357. {
  358. return ERR_PTR(-ESTALE);
  359. }
  360. /**
  361. * export_encode_fh - default export_operations->encode_fh function
  362. * @dentry: the dentry to encode
  363. * @fh: where to store the file handle fragment
  364. * @max_len: maximum length to store there
  365. * @connectable: whether to store parent information
  366. *
  367. * This default encode_fh function assumes that the 32 inode number
  368. * is suitable for locating an inode, and that the generation number
  369. * can be used to check that it is still valid. It places them in the
  370. * filehandle fragment where export_decode_fh expects to find them.
  371. */
  372. static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
  373. int connectable)
  374. {
  375. struct inode * inode = dentry->d_inode;
  376. int len = *max_len;
  377. int type = 1;
  378. if (len < 2 || (connectable && len < 4))
  379. return 255;
  380. len = 2;
  381. fh[0] = inode->i_ino;
  382. fh[1] = inode->i_generation;
  383. if (connectable && !S_ISDIR(inode->i_mode)) {
  384. struct inode *parent;
  385. spin_lock(&dentry->d_lock);
  386. parent = dentry->d_parent->d_inode;
  387. fh[2] = parent->i_ino;
  388. fh[3] = parent->i_generation;
  389. spin_unlock(&dentry->d_lock);
  390. len = 4;
  391. type = 2;
  392. }
  393. *max_len = len;
  394. return type;
  395. }
  396. /**
  397. * export_decode_fh - default export_operations->decode_fh function
  398. * @sb: The superblock
  399. * @fh: pointer to the file handle fragment
  400. * @fh_len: length of file handle fragment
  401. * @acceptable: function for testing acceptability of dentrys
  402. * @context: context for @acceptable
  403. *
  404. * This is the default decode_fh() function.
  405. * a fileid_type of 1 indicates that the filehandlefragment
  406. * just contains an object identifier understood by get_dentry.
  407. * a fileid_type of 2 says that there is also a directory
  408. * identifier 8 bytes in to the filehandlefragement.
  409. */
  410. static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
  411. int fileid_type,
  412. int (*acceptable)(void *context, struct dentry *de),
  413. void *context)
  414. {
  415. __u32 parent[2];
  416. parent[0] = parent[1] = 0;
  417. if (fh_len < 2 || fileid_type > 2)
  418. return NULL;
  419. if (fileid_type == 2) {
  420. if (fh_len > 2) parent[0] = fh[2];
  421. if (fh_len > 3) parent[1] = fh[3];
  422. }
  423. return find_exported_dentry(sb, fh, parent,
  424. acceptable, context);
  425. }
  426. int exportfs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
  427. int connectable)
  428. {
  429. struct export_operations *nop = dentry->d_sb->s_export_op;
  430. return CALL(nop, encode_fh)(dentry, fh, max_len, connectable);
  431. }
  432. EXPORT_SYMBOL_GPL(exportfs_encode_fh);
  433. struct dentry *exportfs_decode_fh(struct vfsmount *mnt, __u32 *fh, int fh_len,
  434. int fileid_type, int (*acceptable)(void *, struct dentry *),
  435. void *context)
  436. {
  437. struct export_operations *nop = mnt->mnt_sb->s_export_op;
  438. return CALL(nop, decode_fh)(mnt->mnt_sb, fh, fh_len, fileid_type,
  439. acceptable, context);
  440. }
  441. EXPORT_SYMBOL_GPL(exportfs_decode_fh);
  442. struct export_operations export_op_default = {
  443. .decode_fh = export_decode_fh,
  444. .encode_fh = export_encode_fh,
  445. .get_name = get_name,
  446. .get_parent = get_parent,
  447. .get_dentry = get_dentry,
  448. };
  449. EXPORT_SYMBOL(find_exported_dentry);
  450. MODULE_LICENSE("GPL");