super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * See COPYING in top-level directory.
  5. */
  6. #include "protocol.h"
  7. #include "orangefs-kernel.h"
  8. #include "orangefs-bufmap.h"
  9. #include <linux/parser.h>
  10. /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
  11. static struct kmem_cache *orangefs_inode_cache;
  12. /* list for storing orangefs specific superblocks in use */
  13. LIST_HEAD(orangefs_superblocks);
  14. DEFINE_SPINLOCK(orangefs_superblocks_lock);
  15. enum {
  16. Opt_intr,
  17. Opt_acl,
  18. Opt_local_lock,
  19. Opt_err
  20. };
  21. static const match_table_t tokens = {
  22. { Opt_acl, "acl" },
  23. { Opt_intr, "intr" },
  24. { Opt_local_lock, "local_lock" },
  25. { Opt_err, NULL }
  26. };
  27. uint64_t orangefs_features;
  28. static int parse_mount_options(struct super_block *sb, char *options,
  29. int silent)
  30. {
  31. struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb);
  32. substring_t args[MAX_OPT_ARGS];
  33. char *p;
  34. /*
  35. * Force any potential flags that might be set from the mount
  36. * to zero, ie, initialize to unset.
  37. */
  38. sb->s_flags &= ~MS_POSIXACL;
  39. orangefs_sb->flags &= ~ORANGEFS_OPT_INTR;
  40. orangefs_sb->flags &= ~ORANGEFS_OPT_LOCAL_LOCK;
  41. while ((p = strsep(&options, ",")) != NULL) {
  42. int token;
  43. if (!*p)
  44. continue;
  45. token = match_token(p, tokens, args);
  46. switch (token) {
  47. case Opt_acl:
  48. sb->s_flags |= MS_POSIXACL;
  49. break;
  50. case Opt_intr:
  51. orangefs_sb->flags |= ORANGEFS_OPT_INTR;
  52. break;
  53. case Opt_local_lock:
  54. orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK;
  55. break;
  56. default:
  57. goto fail;
  58. }
  59. }
  60. return 0;
  61. fail:
  62. if (!silent)
  63. gossip_err("Error: mount option [%s] is not supported.\n", p);
  64. return -EINVAL;
  65. }
  66. static void orangefs_inode_cache_ctor(void *req)
  67. {
  68. struct orangefs_inode_s *orangefs_inode = req;
  69. inode_init_once(&orangefs_inode->vfs_inode);
  70. init_rwsem(&orangefs_inode->xattr_sem);
  71. orangefs_inode->vfs_inode.i_version = 1;
  72. }
  73. static struct inode *orangefs_alloc_inode(struct super_block *sb)
  74. {
  75. struct orangefs_inode_s *orangefs_inode;
  76. orangefs_inode = kmem_cache_alloc(orangefs_inode_cache, GFP_KERNEL);
  77. if (orangefs_inode == NULL) {
  78. gossip_err("Failed to allocate orangefs_inode\n");
  79. return NULL;
  80. }
  81. /*
  82. * We want to clear everything except for rw_semaphore and the
  83. * vfs_inode.
  84. */
  85. memset(&orangefs_inode->refn.khandle, 0, 16);
  86. orangefs_inode->refn.fs_id = ORANGEFS_FS_ID_NULL;
  87. orangefs_inode->last_failed_block_index_read = 0;
  88. memset(orangefs_inode->link_target, 0, sizeof(orangefs_inode->link_target));
  89. orangefs_inode->pinode_flags = 0;
  90. gossip_debug(GOSSIP_SUPER_DEBUG,
  91. "orangefs_alloc_inode: allocated %p\n",
  92. &orangefs_inode->vfs_inode);
  93. return &orangefs_inode->vfs_inode;
  94. }
  95. static void orangefs_destroy_inode(struct inode *inode)
  96. {
  97. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  98. gossip_debug(GOSSIP_SUPER_DEBUG,
  99. "%s: deallocated %p destroying inode %pU\n",
  100. __func__, orangefs_inode, get_khandle_from_ino(inode));
  101. kmem_cache_free(orangefs_inode_cache, orangefs_inode);
  102. }
  103. /*
  104. * NOTE: information filled in here is typically reflected in the
  105. * output of the system command 'df'
  106. */
  107. static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf)
  108. {
  109. int ret = -ENOMEM;
  110. struct orangefs_kernel_op_s *new_op = NULL;
  111. int flags = 0;
  112. struct super_block *sb = NULL;
  113. sb = dentry->d_sb;
  114. gossip_debug(GOSSIP_SUPER_DEBUG,
  115. "orangefs_statfs: called on sb %p (fs_id is %d)\n",
  116. sb,
  117. (int)(ORANGEFS_SB(sb)->fs_id));
  118. new_op = op_alloc(ORANGEFS_VFS_OP_STATFS);
  119. if (!new_op)
  120. return ret;
  121. new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id;
  122. if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR)
  123. flags = ORANGEFS_OP_INTERRUPTIBLE;
  124. ret = service_operation(new_op, "orangefs_statfs", flags);
  125. if (new_op->downcall.status < 0)
  126. goto out_op_release;
  127. gossip_debug(GOSSIP_SUPER_DEBUG,
  128. "%s: got %ld blocks available | "
  129. "%ld blocks total | %ld block size | "
  130. "%ld files total | %ld files avail\n",
  131. __func__,
  132. (long)new_op->downcall.resp.statfs.blocks_avail,
  133. (long)new_op->downcall.resp.statfs.blocks_total,
  134. (long)new_op->downcall.resp.statfs.block_size,
  135. (long)new_op->downcall.resp.statfs.files_total,
  136. (long)new_op->downcall.resp.statfs.files_avail);
  137. buf->f_type = sb->s_magic;
  138. memcpy(&buf->f_fsid, &ORANGEFS_SB(sb)->fs_id, sizeof(buf->f_fsid));
  139. buf->f_bsize = new_op->downcall.resp.statfs.block_size;
  140. buf->f_namelen = ORANGEFS_NAME_MAX;
  141. buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total;
  142. buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
  143. buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
  144. buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total;
  145. buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail;
  146. buf->f_frsize = sb->s_blocksize;
  147. out_op_release:
  148. op_release(new_op);
  149. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_statfs: returning %d\n", ret);
  150. return ret;
  151. }
  152. /*
  153. * Remount as initiated by VFS layer. We just need to reparse the mount
  154. * options, no need to signal pvfs2-client-core about it.
  155. */
  156. static int orangefs_remount_fs(struct super_block *sb, int *flags, char *data)
  157. {
  158. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount_fs: called\n");
  159. return parse_mount_options(sb, data, 1);
  160. }
  161. /*
  162. * Remount as initiated by pvfs2-client-core on restart. This is used to
  163. * repopulate mount information left from previous pvfs2-client-core.
  164. *
  165. * the idea here is that given a valid superblock, we're
  166. * re-initializing the user space client with the initial mount
  167. * information specified when the super block was first initialized.
  168. * this is very different than the first initialization/creation of a
  169. * superblock. we use the special service_priority_operation to make
  170. * sure that the mount gets ahead of any other pending operation that
  171. * is waiting for servicing. this means that the pvfs2-client won't
  172. * fail to start several times for all other pending operations before
  173. * the client regains all of the mount information from us.
  174. * NOTE: this function assumes that the request_mutex is already acquired!
  175. */
  176. int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb)
  177. {
  178. struct orangefs_kernel_op_s *new_op;
  179. int ret = -EINVAL;
  180. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n");
  181. new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
  182. if (!new_op)
  183. return -ENOMEM;
  184. strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
  185. orangefs_sb->devname,
  186. ORANGEFS_MAX_SERVER_ADDR_LEN);
  187. gossip_debug(GOSSIP_SUPER_DEBUG,
  188. "Attempting ORANGEFS Remount via host %s\n",
  189. new_op->upcall.req.fs_mount.orangefs_config_server);
  190. /*
  191. * we assume that the calling function has already acquired the
  192. * request_mutex to prevent other operations from bypassing
  193. * this one
  194. */
  195. ret = service_operation(new_op, "orangefs_remount",
  196. ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
  197. gossip_debug(GOSSIP_SUPER_DEBUG,
  198. "orangefs_remount: mount got return value of %d\n",
  199. ret);
  200. if (ret == 0) {
  201. /*
  202. * store the id assigned to this sb -- it's just a
  203. * short-lived mapping that the system interface uses
  204. * to map this superblock to a particular mount entry
  205. */
  206. orangefs_sb->id = new_op->downcall.resp.fs_mount.id;
  207. orangefs_sb->mount_pending = 0;
  208. }
  209. op_release(new_op);
  210. if (orangefs_userspace_version >= 20906) {
  211. new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
  212. if (!new_op)
  213. return -ENOMEM;
  214. new_op->upcall.req.features.features = 0;
  215. ret = service_operation(new_op, "orangefs_features", 0);
  216. orangefs_features = new_op->downcall.resp.features.features;
  217. op_release(new_op);
  218. } else {
  219. orangefs_features = 0;
  220. }
  221. return ret;
  222. }
  223. int fsid_key_table_initialize(void)
  224. {
  225. return 0;
  226. }
  227. void fsid_key_table_finalize(void)
  228. {
  229. }
  230. /* Called whenever the VFS dirties the inode in response to atime updates */
  231. static void orangefs_dirty_inode(struct inode *inode, int flags)
  232. {
  233. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  234. gossip_debug(GOSSIP_SUPER_DEBUG,
  235. "orangefs_dirty_inode: %pU\n",
  236. get_khandle_from_ino(inode));
  237. SetAtimeFlag(orangefs_inode);
  238. }
  239. static const struct super_operations orangefs_s_ops = {
  240. .alloc_inode = orangefs_alloc_inode,
  241. .destroy_inode = orangefs_destroy_inode,
  242. .dirty_inode = orangefs_dirty_inode,
  243. .drop_inode = generic_delete_inode,
  244. .statfs = orangefs_statfs,
  245. .remount_fs = orangefs_remount_fs,
  246. .show_options = generic_show_options,
  247. };
  248. static struct dentry *orangefs_fh_to_dentry(struct super_block *sb,
  249. struct fid *fid,
  250. int fh_len,
  251. int fh_type)
  252. {
  253. struct orangefs_object_kref refn;
  254. if (fh_len < 5 || fh_type > 2)
  255. return NULL;
  256. ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16);
  257. refn.fs_id = (u32) fid->raw[4];
  258. gossip_debug(GOSSIP_SUPER_DEBUG,
  259. "fh_to_dentry: handle %pU, fs_id %d\n",
  260. &refn.khandle,
  261. refn.fs_id);
  262. return d_obtain_alias(orangefs_iget(sb, &refn));
  263. }
  264. static int orangefs_encode_fh(struct inode *inode,
  265. __u32 *fh,
  266. int *max_len,
  267. struct inode *parent)
  268. {
  269. int len = parent ? 10 : 5;
  270. int type = 1;
  271. struct orangefs_object_kref refn;
  272. if (*max_len < len) {
  273. gossip_lerr("fh buffer is too small for encoding\n");
  274. *max_len = len;
  275. type = 255;
  276. goto out;
  277. }
  278. refn = ORANGEFS_I(inode)->refn;
  279. ORANGEFS_khandle_to(&refn.khandle, fh, 16);
  280. fh[4] = refn.fs_id;
  281. gossip_debug(GOSSIP_SUPER_DEBUG,
  282. "Encoding fh: handle %pU, fsid %u\n",
  283. &refn.khandle,
  284. refn.fs_id);
  285. if (parent) {
  286. refn = ORANGEFS_I(parent)->refn;
  287. ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16);
  288. fh[9] = refn.fs_id;
  289. type = 2;
  290. gossip_debug(GOSSIP_SUPER_DEBUG,
  291. "Encoding parent: handle %pU, fsid %u\n",
  292. &refn.khandle,
  293. refn.fs_id);
  294. }
  295. *max_len = len;
  296. out:
  297. return type;
  298. }
  299. static const struct export_operations orangefs_export_ops = {
  300. .encode_fh = orangefs_encode_fh,
  301. .fh_to_dentry = orangefs_fh_to_dentry,
  302. };
  303. static int orangefs_fill_sb(struct super_block *sb,
  304. struct orangefs_fs_mount_response *fs_mount,
  305. void *data, int silent)
  306. {
  307. int ret = -EINVAL;
  308. struct inode *root = NULL;
  309. struct dentry *root_dentry = NULL;
  310. struct orangefs_object_kref root_object;
  311. /* alloc and init our private orangefs sb info */
  312. sb->s_fs_info = kzalloc(sizeof(struct orangefs_sb_info_s), GFP_KERNEL);
  313. if (!ORANGEFS_SB(sb))
  314. return -ENOMEM;
  315. ORANGEFS_SB(sb)->sb = sb;
  316. ORANGEFS_SB(sb)->root_khandle = fs_mount->root_khandle;
  317. ORANGEFS_SB(sb)->fs_id = fs_mount->fs_id;
  318. ORANGEFS_SB(sb)->id = fs_mount->id;
  319. if (data) {
  320. ret = parse_mount_options(sb, data, silent);
  321. if (ret)
  322. return ret;
  323. }
  324. /* Hang the xattr handlers off the superblock */
  325. sb->s_xattr = orangefs_xattr_handlers;
  326. sb->s_magic = ORANGEFS_SUPER_MAGIC;
  327. sb->s_op = &orangefs_s_ops;
  328. sb->s_d_op = &orangefs_dentry_operations;
  329. sb->s_blocksize = orangefs_bufmap_size_query();
  330. sb->s_blocksize_bits = orangefs_bufmap_shift_query();
  331. sb->s_maxbytes = MAX_LFS_FILESIZE;
  332. root_object.khandle = ORANGEFS_SB(sb)->root_khandle;
  333. root_object.fs_id = ORANGEFS_SB(sb)->fs_id;
  334. gossip_debug(GOSSIP_SUPER_DEBUG,
  335. "get inode %pU, fsid %d\n",
  336. &root_object.khandle,
  337. root_object.fs_id);
  338. root = orangefs_iget(sb, &root_object);
  339. if (IS_ERR(root))
  340. return PTR_ERR(root);
  341. gossip_debug(GOSSIP_SUPER_DEBUG,
  342. "Allocated root inode [%p] with mode %x\n",
  343. root,
  344. root->i_mode);
  345. /* allocates and places root dentry in dcache */
  346. root_dentry = d_make_root(root);
  347. if (!root_dentry)
  348. return -ENOMEM;
  349. sb->s_export_op = &orangefs_export_ops;
  350. sb->s_root = root_dentry;
  351. return 0;
  352. }
  353. struct dentry *orangefs_mount(struct file_system_type *fst,
  354. int flags,
  355. const char *devname,
  356. void *data)
  357. {
  358. int ret = -EINVAL;
  359. struct super_block *sb = ERR_PTR(-EINVAL);
  360. struct orangefs_kernel_op_s *new_op;
  361. struct dentry *d = ERR_PTR(-EINVAL);
  362. gossip_debug(GOSSIP_SUPER_DEBUG,
  363. "orangefs_mount: called with devname %s\n",
  364. devname);
  365. if (!devname) {
  366. gossip_err("ERROR: device name not specified.\n");
  367. return ERR_PTR(-EINVAL);
  368. }
  369. new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
  370. if (!new_op)
  371. return ERR_PTR(-ENOMEM);
  372. strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
  373. devname,
  374. ORANGEFS_MAX_SERVER_ADDR_LEN);
  375. gossip_debug(GOSSIP_SUPER_DEBUG,
  376. "Attempting ORANGEFS Mount via host %s\n",
  377. new_op->upcall.req.fs_mount.orangefs_config_server);
  378. ret = service_operation(new_op, "orangefs_mount", 0);
  379. gossip_debug(GOSSIP_SUPER_DEBUG,
  380. "orangefs_mount: mount got return value of %d\n", ret);
  381. if (ret)
  382. goto free_op;
  383. if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) {
  384. gossip_err("ERROR: Retrieved null fs_id\n");
  385. ret = -EINVAL;
  386. goto free_op;
  387. }
  388. sb = sget(fst, NULL, set_anon_super, flags, NULL);
  389. if (IS_ERR(sb)) {
  390. d = ERR_CAST(sb);
  391. goto free_op;
  392. }
  393. ret = orangefs_fill_sb(sb,
  394. &new_op->downcall.resp.fs_mount, data,
  395. flags & MS_SILENT ? 1 : 0);
  396. if (ret) {
  397. d = ERR_PTR(ret);
  398. goto free_op;
  399. }
  400. /*
  401. * on successful mount, store the devname and data
  402. * used
  403. */
  404. strncpy(ORANGEFS_SB(sb)->devname,
  405. devname,
  406. ORANGEFS_MAX_SERVER_ADDR_LEN);
  407. /* mount_pending must be cleared */
  408. ORANGEFS_SB(sb)->mount_pending = 0;
  409. /*
  410. * finally, add this sb to our list of known orangefs
  411. * sb's
  412. */
  413. gossip_debug(GOSSIP_SUPER_DEBUG,
  414. "Adding SB %p to orangefs superblocks\n",
  415. ORANGEFS_SB(sb));
  416. spin_lock(&orangefs_superblocks_lock);
  417. list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks);
  418. spin_unlock(&orangefs_superblocks_lock);
  419. op_release(new_op);
  420. if (orangefs_userspace_version >= 20906) {
  421. new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
  422. if (!new_op)
  423. return ERR_PTR(-ENOMEM);
  424. new_op->upcall.req.features.features = 0;
  425. ret = service_operation(new_op, "orangefs_features", 0);
  426. orangefs_features = new_op->downcall.resp.features.features;
  427. op_release(new_op);
  428. } else {
  429. orangefs_features = 0;
  430. }
  431. return dget(sb->s_root);
  432. free_op:
  433. gossip_err("orangefs_mount: mount request failed with %d\n", ret);
  434. if (ret == -EINVAL) {
  435. gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
  436. gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
  437. }
  438. op_release(new_op);
  439. return d;
  440. }
  441. void orangefs_kill_sb(struct super_block *sb)
  442. {
  443. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n");
  444. /* provided sb cleanup */
  445. kill_anon_super(sb);
  446. /*
  447. * issue the unmount to userspace to tell it to remove the
  448. * dynamic mount info it has for this superblock
  449. */
  450. orangefs_unmount_sb(sb);
  451. /* remove the sb from our list of orangefs specific sb's */
  452. spin_lock(&orangefs_superblocks_lock);
  453. __list_del_entry(&ORANGEFS_SB(sb)->list); /* not list_del_init */
  454. ORANGEFS_SB(sb)->list.prev = NULL;
  455. spin_unlock(&orangefs_superblocks_lock);
  456. /*
  457. * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
  458. * gets completed before we free the dang thing.
  459. */
  460. mutex_lock(&orangefs_request_mutex);
  461. mutex_unlock(&orangefs_request_mutex);
  462. /* free the orangefs superblock private data */
  463. kfree(ORANGEFS_SB(sb));
  464. }
  465. int orangefs_inode_cache_initialize(void)
  466. {
  467. orangefs_inode_cache = kmem_cache_create("orangefs_inode_cache",
  468. sizeof(struct orangefs_inode_s),
  469. 0,
  470. ORANGEFS_CACHE_CREATE_FLAGS,
  471. orangefs_inode_cache_ctor);
  472. if (!orangefs_inode_cache) {
  473. gossip_err("Cannot create orangefs_inode_cache\n");
  474. return -ENOMEM;
  475. }
  476. return 0;
  477. }
  478. int orangefs_inode_cache_finalize(void)
  479. {
  480. kmem_cache_destroy(orangefs_inode_cache);
  481. return 0;
  482. }