0001
0002
0003
0004
0005 #include <linux/init.h>
0006 #include <linux/module.h>
0007 #include <linux/mount.h>
0008 #include <linux/nfs4_mount.h>
0009 #include <linux/nfs_fs.h>
0010 #include <linux/nfs_ssc.h>
0011 #include "delegation.h"
0012 #include "internal.h"
0013 #include "nfs4_fs.h"
0014 #include "nfs4idmap.h"
0015 #include "dns_resolve.h"
0016 #include "pnfs.h"
0017 #include "nfs.h"
0018
0019 #define NFSDBG_FACILITY NFSDBG_VFS
0020
0021 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
0022 static void nfs4_evict_inode(struct inode *inode);
0023
0024 static const struct super_operations nfs4_sops = {
0025 .alloc_inode = nfs_alloc_inode,
0026 .free_inode = nfs_free_inode,
0027 .write_inode = nfs4_write_inode,
0028 .drop_inode = nfs_drop_inode,
0029 .statfs = nfs_statfs,
0030 .evict_inode = nfs4_evict_inode,
0031 .umount_begin = nfs_umount_begin,
0032 .show_options = nfs_show_options,
0033 .show_devname = nfs_show_devname,
0034 .show_path = nfs_show_path,
0035 .show_stats = nfs_show_stats,
0036 };
0037
0038 struct nfs_subversion nfs_v4 = {
0039 .owner = THIS_MODULE,
0040 .nfs_fs = &nfs4_fs_type,
0041 .rpc_vers = &nfs_version4,
0042 .rpc_ops = &nfs_v4_clientops,
0043 .sops = &nfs4_sops,
0044 .xattr = nfs4_xattr_handlers,
0045 };
0046
0047 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
0048 {
0049 int ret = nfs_write_inode(inode, wbc);
0050
0051 if (ret == 0)
0052 ret = pnfs_layoutcommit_inode(inode,
0053 wbc->sync_mode == WB_SYNC_ALL);
0054 return ret;
0055 }
0056
0057
0058
0059
0060
0061
0062 static void nfs4_evict_inode(struct inode *inode)
0063 {
0064 truncate_inode_pages_final(&inode->i_data);
0065 clear_inode(inode);
0066
0067 nfs_inode_evict_delegation(inode);
0068
0069 pnfs_return_layout(inode);
0070 pnfs_destroy_layout_final(NFS_I(inode));
0071
0072 nfs_clear_inode(inode);
0073 nfs4_xattr_cache_zap(inode);
0074 }
0075
0076 struct nfs_referral_count {
0077 struct list_head list;
0078 const struct task_struct *task;
0079 unsigned int referral_count;
0080 };
0081
0082 static LIST_HEAD(nfs_referral_count_list);
0083 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
0084
0085 static struct nfs_referral_count *nfs_find_referral_count(void)
0086 {
0087 struct nfs_referral_count *p;
0088
0089 list_for_each_entry(p, &nfs_referral_count_list, list) {
0090 if (p->task == current)
0091 return p;
0092 }
0093 return NULL;
0094 }
0095
0096 #define NFS_MAX_NESTED_REFERRALS 2
0097
0098 static int nfs_referral_loop_protect(void)
0099 {
0100 struct nfs_referral_count *p, *new;
0101 int ret = -ENOMEM;
0102
0103 new = kmalloc(sizeof(*new), GFP_KERNEL);
0104 if (!new)
0105 goto out;
0106 new->task = current;
0107 new->referral_count = 1;
0108
0109 ret = 0;
0110 spin_lock(&nfs_referral_count_list_lock);
0111 p = nfs_find_referral_count();
0112 if (p != NULL) {
0113 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
0114 ret = -ELOOP;
0115 else
0116 p->referral_count++;
0117 } else {
0118 list_add(&new->list, &nfs_referral_count_list);
0119 new = NULL;
0120 }
0121 spin_unlock(&nfs_referral_count_list_lock);
0122 kfree(new);
0123 out:
0124 return ret;
0125 }
0126
0127 static void nfs_referral_loop_unprotect(void)
0128 {
0129 struct nfs_referral_count *p;
0130
0131 spin_lock(&nfs_referral_count_list_lock);
0132 p = nfs_find_referral_count();
0133 p->referral_count--;
0134 if (p->referral_count == 0)
0135 list_del(&p->list);
0136 else
0137 p = NULL;
0138 spin_unlock(&nfs_referral_count_list_lock);
0139 kfree(p);
0140 }
0141
0142 static int do_nfs4_mount(struct nfs_server *server,
0143 struct fs_context *fc,
0144 const char *hostname,
0145 const char *export_path)
0146 {
0147 struct nfs_fs_context *root_ctx;
0148 struct fs_context *root_fc;
0149 struct vfsmount *root_mnt;
0150 struct dentry *dentry;
0151 size_t len;
0152 int ret;
0153
0154 struct fs_parameter param = {
0155 .key = "source",
0156 .type = fs_value_is_string,
0157 .dirfd = -1,
0158 };
0159
0160 if (IS_ERR(server))
0161 return PTR_ERR(server);
0162
0163 root_fc = vfs_dup_fs_context(fc);
0164 if (IS_ERR(root_fc)) {
0165 nfs_free_server(server);
0166 return PTR_ERR(root_fc);
0167 }
0168 kfree(root_fc->source);
0169 root_fc->source = NULL;
0170
0171 root_ctx = nfs_fc2context(root_fc);
0172 root_ctx->internal = true;
0173 root_ctx->server = server;
0174
0175
0176 len = strlen(hostname) + 5;
0177 param.string = kmalloc(len, GFP_KERNEL);
0178 if (param.string == NULL) {
0179 put_fs_context(root_fc);
0180 return -ENOMEM;
0181 }
0182
0183
0184 if (strchr(hostname, ':'))
0185 param.size = snprintf(param.string, len, "[%s]:/", hostname);
0186 else
0187 param.size = snprintf(param.string, len, "%s:/", hostname);
0188 ret = vfs_parse_fs_param(root_fc, ¶m);
0189 kfree(param.string);
0190 if (ret < 0) {
0191 put_fs_context(root_fc);
0192 return ret;
0193 }
0194 root_mnt = fc_mount(root_fc);
0195 put_fs_context(root_fc);
0196
0197 if (IS_ERR(root_mnt))
0198 return PTR_ERR(root_mnt);
0199
0200 ret = nfs_referral_loop_protect();
0201 if (ret) {
0202 mntput(root_mnt);
0203 return ret;
0204 }
0205
0206 dentry = mount_subtree(root_mnt, export_path);
0207 nfs_referral_loop_unprotect();
0208
0209 if (IS_ERR(dentry))
0210 return PTR_ERR(dentry);
0211
0212 fc->root = dentry;
0213 return 0;
0214 }
0215
0216 int nfs4_try_get_tree(struct fs_context *fc)
0217 {
0218 struct nfs_fs_context *ctx = nfs_fc2context(fc);
0219 int err;
0220
0221 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
0222
0223
0224
0225
0226 err= do_nfs4_mount(nfs4_create_server(fc),
0227 fc, ctx->nfs_server.hostname,
0228 ctx->nfs_server.export_path);
0229 if (err) {
0230 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
0231 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
0232 } else {
0233 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
0234 }
0235 return err;
0236 }
0237
0238
0239
0240
0241 int nfs4_get_referral_tree(struct fs_context *fc)
0242 {
0243 struct nfs_fs_context *ctx = nfs_fc2context(fc);
0244 int err;
0245
0246 dprintk("--> nfs4_referral_mount()\n");
0247
0248
0249 err = do_nfs4_mount(nfs4_create_referral_server(fc),
0250 fc, ctx->nfs_server.hostname,
0251 ctx->nfs_server.export_path);
0252 if (err) {
0253 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
0254 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
0255 } else {
0256 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
0257 }
0258 return err;
0259 }
0260
0261 static int __init init_nfs_v4(void)
0262 {
0263 int err;
0264
0265 err = nfs_dns_resolver_init();
0266 if (err)
0267 goto out;
0268
0269 err = nfs_idmap_init();
0270 if (err)
0271 goto out1;
0272
0273 #ifdef CONFIG_NFS_V4_2
0274 err = nfs4_xattr_cache_init();
0275 if (err)
0276 goto out2;
0277 #endif
0278
0279 err = nfs4_register_sysctl();
0280 if (err)
0281 goto out2;
0282
0283 #ifdef CONFIG_NFS_V4_2
0284 nfs42_ssc_register_ops();
0285 #endif
0286 register_nfs_version(&nfs_v4);
0287 return 0;
0288 out2:
0289 nfs_idmap_quit();
0290 out1:
0291 nfs_dns_resolver_destroy();
0292 out:
0293 return err;
0294 }
0295
0296 static void __exit exit_nfs_v4(void)
0297 {
0298
0299 nfs4_pnfs_v3_ds_connect_unload();
0300
0301 unregister_nfs_version(&nfs_v4);
0302 #ifdef CONFIG_NFS_V4_2
0303 nfs4_xattr_cache_exit();
0304 nfs42_ssc_unregister_ops();
0305 #endif
0306 nfs4_unregister_sysctl();
0307 nfs_idmap_quit();
0308 nfs_dns_resolver_destroy();
0309 }
0310
0311 MODULE_LICENSE("GPL");
0312
0313 module_init(init_nfs_v4);
0314 module_exit(exit_nfs_v4);