123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- #include <linux/dcache.h>
- #include <linux/mount.h>
- #include <linux/namei.h>
- #include <linux/slab.h>
- #include <linux/vfs.h>
- #include <linux/fs.h>
- #include <linux/inet.h>
- #include "cifsglob.h"
- #include "cifsproto.h"
- #include "cifsfs.h"
- #include "dns_resolve.h"
- #include "cifs_debug.h"
- #include "cifs_unicode.h"
- static LIST_HEAD(cifs_dfs_automount_list);
- static void cifs_dfs_expire_automounts(struct work_struct *work);
- static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
- cifs_dfs_expire_automounts);
- static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
- static void cifs_dfs_expire_automounts(struct work_struct *work)
- {
- struct list_head *list = &cifs_dfs_automount_list;
- mark_mounts_for_expiry(list);
- if (!list_empty(list))
- schedule_delayed_work(&cifs_dfs_automount_task,
- cifs_dfs_mountpoint_expiry_timeout);
- }
- void cifs_dfs_release_automount_timer(void)
- {
- BUG_ON(!list_empty(&cifs_dfs_automount_list));
- cancel_delayed_work_sync(&cifs_dfs_automount_task);
- }
- static char *
- cifs_build_devname(char *nodename, const char *prepath)
- {
- size_t pplen;
- size_t unclen;
- char *dev;
- char *pos;
-
- nodename += strspn(nodename, "\\");
- if (!*nodename)
- return ERR_PTR(-EINVAL);
-
- unclen = strlen(nodename);
- pos = nodename + unclen - 1;
-
- while (*pos == '\\') {
- --pos;
- --unclen;
- }
-
- pplen = prepath ? strlen(prepath) : 0;
- dev = kmalloc(2 + unclen + 1 + pplen + 1, GFP_KERNEL);
- if (!dev)
- return ERR_PTR(-ENOMEM);
- pos = dev;
-
- *pos = '/';
- ++pos;
- *pos = '/';
- ++pos;
-
- memcpy(pos, nodename, unclen);
- pos += unclen;
-
- if (pplen) {
- *pos = '/';
- ++pos;
- memcpy(pos, prepath, pplen);
- pos += pplen;
- }
-
- *pos = '\0';
- convert_delimiter(dev, '/');
- return dev;
- }
- char *cifs_compose_mount_options(const char *sb_mountdata,
- const char *fullpath,
- const struct dfs_info3_param *ref,
- char **devname)
- {
- int rc;
- char *mountdata = NULL;
- const char *prepath = NULL;
- int md_len;
- char *tkn_e;
- char *srvIP = NULL;
- char sep = ',';
- int off, noff;
- if (sb_mountdata == NULL)
- return ERR_PTR(-EINVAL);
- if (strlen(fullpath) - ref->path_consumed) {
- prepath = fullpath + ref->path_consumed;
-
- if (*prepath == '/' || *prepath == '\\')
- prepath++;
- }
- *devname = cifs_build_devname(ref->node_name, prepath);
- if (IS_ERR(*devname)) {
- rc = PTR_ERR(*devname);
- *devname = NULL;
- goto compose_mount_options_err;
- }
- rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
- if (rc < 0) {
- cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
- __func__, *devname, rc);
- goto compose_mount_options_err;
- }
-
- md_len = strlen(sb_mountdata) + INET6_ADDRSTRLEN;
- mountdata = kzalloc(md_len + sizeof("ip=") + 1, GFP_KERNEL);
- if (mountdata == NULL) {
- rc = -ENOMEM;
- goto compose_mount_options_err;
- }
-
- off = 0;
- if (strncmp(sb_mountdata, "sep=", 4) == 0) {
- sep = sb_mountdata[4];
- strncpy(mountdata, sb_mountdata, 5);
- off += 5;
- }
- do {
- tkn_e = strchr(sb_mountdata + off, sep);
- if (tkn_e == NULL)
- noff = strlen(sb_mountdata + off);
- else
- noff = tkn_e - (sb_mountdata + off) + 1;
- if (strncasecmp(sb_mountdata + off, "unc=", 4) == 0) {
- off += noff;
- continue;
- }
- if (strncasecmp(sb_mountdata + off, "ip=", 3) == 0) {
- off += noff;
- continue;
- }
- if (strncasecmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
- off += noff;
- continue;
- }
- strncat(mountdata, sb_mountdata + off, noff);
- off += noff;
- } while (tkn_e);
- strcat(mountdata, sb_mountdata + off);
- mountdata[md_len] = '\0';
-
- if (mountdata[strlen(mountdata) - 1] != sep)
- strncat(mountdata, &sep, 1);
- strcat(mountdata, "ip=");
- strcat(mountdata, srvIP);
-
-
- compose_mount_options_out:
- kfree(srvIP);
- return mountdata;
- compose_mount_options_err:
- kfree(mountdata);
- mountdata = ERR_PTR(rc);
- kfree(*devname);
- *devname = NULL;
- goto compose_mount_options_out;
- }
- static struct vfsmount *cifs_dfs_do_refmount(struct dentry *mntpt,
- struct cifs_sb_info *cifs_sb,
- const char *fullpath, const struct dfs_info3_param *ref)
- {
- struct vfsmount *mnt;
- char *mountdata;
- char *devname = NULL;
-
- mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
- fullpath + 1, ref, &devname);
- if (IS_ERR(mountdata))
- return (struct vfsmount *)mountdata;
- mnt = vfs_submount(mntpt, &cifs_fs_type, devname, mountdata);
- kfree(mountdata);
- kfree(devname);
- return mnt;
- }
- static void dump_referral(const struct dfs_info3_param *ref)
- {
- cifs_dbg(FYI, "DFS: ref path: %s\n", ref->path_name);
- cifs_dbg(FYI, "DFS: node path: %s\n", ref->node_name);
- cifs_dbg(FYI, "DFS: fl: %d, srv_type: %d\n",
- ref->flags, ref->server_type);
- cifs_dbg(FYI, "DFS: ref_flags: %d, path_consumed: %d\n",
- ref->ref_flag, ref->path_consumed);
- }
- static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
- {
- struct dfs_info3_param *referrals = NULL;
- unsigned int num_referrals = 0;
- struct cifs_sb_info *cifs_sb;
- struct cifs_ses *ses;
- char *full_path;
- unsigned int xid;
- int i;
- int rc;
- struct vfsmount *mnt;
- struct tcon_link *tlink;
- cifs_dbg(FYI, "in %s\n", __func__);
- BUG_ON(IS_ROOT(mntpt));
-
- mnt = ERR_PTR(-ENOMEM);
- full_path = build_path_from_dentry(mntpt);
- if (full_path == NULL)
- goto cdda_exit;
- cifs_sb = CIFS_SB(mntpt->d_sb);
- tlink = cifs_sb_tlink(cifs_sb);
- if (IS_ERR(tlink)) {
- mnt = ERR_CAST(tlink);
- goto free_full_path;
- }
- ses = tlink_tcon(tlink)->ses;
- xid = get_xid();
- rc = get_dfs_path(xid, ses, full_path + 1, cifs_sb->local_nls,
- &num_referrals, &referrals,
- cifs_remap(cifs_sb));
- free_xid(xid);
- cifs_put_tlink(tlink);
- mnt = ERR_PTR(-ENOENT);
- for (i = 0; i < num_referrals; i++) {
- int len;
- dump_referral(referrals + i);
-
- len = strlen(referrals[i].node_name);
- if (len < 2) {
- cifs_dbg(VFS, "%s: Net Address path too short: %s\n",
- __func__, referrals[i].node_name);
- mnt = ERR_PTR(-EINVAL);
- break;
- }
- mnt = cifs_dfs_do_refmount(mntpt, cifs_sb,
- full_path, referrals + i);
- cifs_dbg(FYI, "%s: cifs_dfs_do_refmount:%s , mnt:%p\n",
- __func__, referrals[i].node_name, mnt);
- if (!IS_ERR(mnt))
- goto success;
- }
-
- if (rc != 0)
- mnt = ERR_PTR(rc);
- success:
- free_dfs_info_array(referrals, num_referrals);
- free_full_path:
- kfree(full_path);
- cdda_exit:
- cifs_dbg(FYI, "leaving %s\n" , __func__);
- return mnt;
- }
- struct vfsmount *cifs_dfs_d_automount(struct path *path)
- {
- struct vfsmount *newmnt;
- cifs_dbg(FYI, "in %s\n", __func__);
- newmnt = cifs_dfs_do_automount(path->dentry);
- if (IS_ERR(newmnt)) {
- cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
- return newmnt;
- }
- mntget(newmnt);
- mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
- schedule_delayed_work(&cifs_dfs_automount_task,
- cifs_dfs_mountpoint_expiry_timeout);
- cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
- return newmnt;
- }
- const struct inode_operations cifs_dfs_referral_inode_operations = {
- };
|