expfs.c 14 KB

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