expfs.c 14 KB

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