Whamcloud - gitweb
A mostly-fix for "mknod /mnt/lustre/foofo p". It doesn't fail outright
[fs/lustre-release.git] / lustre / llite / namei.c
index 5d23512..39214e4 100644 (file)
@@ -44,7 +44,7 @@ extern int ll_setattr(struct dentry *de, struct iattr *attr);
 
 /* from dir.c */
 extern int ll_add_link (struct dentry *dentry, struct inode *inode);
-ino_t ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
+obd_id ll_inode_by_name(struct inode * dir, struct dentry *dentry, int *typ);
 int ext2_make_empty(struct inode *inode, struct inode *parent);
 struct ext2_dir_entry_2 * ext2_find_entry (struct inode * dir,
                    struct dentry *dentry, struct page ** res_page);
@@ -98,30 +98,32 @@ int ll_lock(struct inode *dir, struct dentry *dentry,
             struct lookup_intent *it, struct lustre_handle *lockh)
 {
         struct ll_sb_info *sbi = ll_i2sbi(dir);
-        int err;
+        int err, lock_mode;
 
         if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_SETATTR |
-                          IT_MKNOD)) )
-                err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT,
-                                  it, LCK_PW, dir, dentry, lockh, 0, NULL, 0,
-                                  dir, sizeof(*dir));
-        else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN))
-                err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT,
-                                  it, LCK_PR, dir, dentry, lockh, 0, NULL, 0,
-                                  dir, sizeof(*dir));
-        else
+                          IT_MKNOD)))
+                lock_mode = LCK_PW;
+        else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_UNLINK |
+                              IT_RMDIR | IT_RENAME | IT_RENAME2))
+                lock_mode = LCK_PR;
+        else if (it->it_op & IT_LOOKUP)
+                lock_mode = LCK_CR;
+        else {
                 LBUG();
+                RETURN(-1);
+        }
+
+        err = mdc_enqueue(&sbi->ll_mdc_conn, LDLM_MDSINTENT, it, lock_mode, dir,
+                          dentry, lockh, 0, NULL, 0, dir, sizeof(*dir));
 
         RETURN(err);
 }
 
 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
 {
-        struct ldlm_lock *lock;
         ENTRY;
 
-        lock = lustre_handle2object(lockh);
-        ldlm_lock_decref(lock, mode);
+        ldlm_lock_decref(lockh, mode);
 
         RETURN(0);
 }
@@ -135,10 +137,16 @@ static struct dentry *ll_lookup2(struct inode * dir, struct dentry *dentry,
         struct ll_inode_md md;
         struct lustre_handle lockh;
         int err, type, offset;
-        ino_t ino;
+        struct lookup_intent lookup_it = { IT_LOOKUP };
+        obd_id ino;
 
         ENTRY;
 
+        if (it == NULL) {
+                it = &lookup_it;
+                dentry->d_it = it;
+        }
+
         CDEBUG(D_INFO, "name: %*s, intent op: %d\n", dentry->d_name.len,
                dentry->d_name.name, it->it_op);
 
@@ -146,71 +154,81 @@ static struct dentry *ll_lookup2(struct inode * dir, struct dentry *dentry,
                 RETURN(ERR_PTR(-ENAMETOOLONG));
 
         err = ll_lock(dir, dentry, it, &lockh);
+        if (err < 0) {
+                /* FIXME: Mike LBUG() can disappear the moment that 
+                 *   ll_lock has sane interrupt behavior 
+                 */
+                LBUG();
+                RETURN(ERR_PTR(err));
+        }
         memcpy(it->it_lock_handle, &lockh, sizeof(lockh));
 
-        if ( (it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) &&
-             it->it_disposition && !it->it_status)
+        if ((it->it_op & (IT_CREAT | IT_MKDIR | IT_SYMLINK | IT_MKNOD)) &&
+            it->it_disposition && !it->it_status)
                 GOTO(negative, NULL);
 
-        if ( (it->it_op & (IT_GETATTR)) &&
-             it->it_disposition && it->it_status)
+        if ((it->it_op & (IT_RENAME | IT_GETATTR | IT_UNLINK | IT_RMDIR |
+                          IT_SETATTR | IT_LOOKUP)) && 
+            it->it_disposition && it->it_status)
                 GOTO(negative, NULL);
 
+        request = (struct ptlrpc_request *)it->it_data;
         if (!it->it_disposition) {
                 struct ll_inode_info *lli = ll_i2info(dir);
                 memcpy(&lli->lli_intent_lock_handle, &lockh, sizeof(lockh));
 
                 ino = ll_inode_by_name(dir, dentry, &type);
+#warning FIXME: handle negative inode case (see old ll_lookup)
 
                 err = mdc_getattr(&sbi->ll_mdc_conn, ino, type,
                                   OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
                 if (err) {
-                        CERROR("failure %d inode %ld\n", err, (long)ino);
+                        CERROR("failure %d inode %Ld\n", err, (long long)ino);
                         ptlrpc_free_req(request);
                         RETURN(ERR_PTR(-abs(err)));
                 }
                 offset = 0;
+        } else if (it->it_op == IT_RENAME2) {
+                inode = ((struct dentry *)(it->it_data))->d_inode;
+                GOTO(out_req, NULL); 
         } else {
-                struct mds_body *body;
-
                 offset = 1;
-                request = (struct ptlrpc_request *)it->it_data;
-                body = lustre_msg_buf(request->rq_repmsg, 1);
-                type = body->mode;
         }
 
-        if (S_ISREG(type)) {
-                if (request->rq_repmsg->bufcount < offset + 2 ||
-                    request->rq_repmsg->buflens[offset + 1] !=
-                    sizeof(struct obdo))
+        md.body = lustre_msg_buf(request->rq_repmsg, offset);
+        if (S_ISREG(md.body->mode)) {
+                if (request->rq_repmsg->bufcount < offset + 1)
                         LBUG();
-
-                md.obdo = lustre_msg_buf(request->rq_repmsg, offset + 1);
+                md.md = lustre_msg_buf(request->rq_repmsg, offset + 1);
         } else
-                md.obdo = NULL;
-
-        md.body = lustre_msg_buf(request->rq_repmsg, offset);
+                md.md = NULL;
 
+        /* No rpc's happen during iget4, -ENOMEM's are possible */
         inode = iget4(dir->i_sb, ino, ll_find_inode, &md);
-
         if (it->it_op & IT_RENAME)
                 it->it_data = dentry;
 
+ out_req:
         ptlrpc_free_req(request);
-        if (!inode)
+        if (!inode || IS_ERR(inode)) { 
+                ll_intent_release(dentry); 
                 RETURN(ERR_PTR(-ENOMEM));
-
+        }
         EXIT;
  negative:
         dentry->d_op = &ll_d_ops;
         d_add(dentry, inode);
+        if (it->it_op == IT_LOOKUP)
+                ll_intent_release(dentry);
+
         return NULL;
 }
 
 static struct inode *ll_create_node(struct inode *dir, const char *name,
                                     int namelen, const char *tgt, int tgtlen,
                                     int mode, __u64 extra,
-                                    struct lookup_intent *it, struct obdo *obdo)
+                                    struct lookup_intent *it,
+                                    struct lov_stripe_md *smd)
 {
         struct inode *inode;
         struct ptlrpc_request *request = NULL;
@@ -218,42 +236,49 @@ static struct inode *ll_create_node(struct inode *dir, const char *name,
         int rc;
         time_t time = CURRENT_TIME;
         struct ll_sb_info *sbi = ll_i2sbi(dir);
+        int gid = current->fsgid;
         struct ll_inode_md md;
 
         ENTRY;
 
+        if (dir->i_mode & S_ISGID) {
+                gid = dir->i_gid;
+                if (S_ISDIR(mode))
+                        mode |= S_ISGID;
+        }
+
         if (!it->it_disposition) {
                 rc = mdc_create(&sbi->ll_mdc_conn, dir, name, namelen, tgt,
                                  tgtlen, mode, current->fsuid,
-                                 current->fsgid, time, extra, obdo, &request);
+                                 gid, time, extra, smd, &request);
                 if (rc) {
                         inode = ERR_PTR(rc);
                         GOTO(out, rc);
                 }
                 body = lustre_msg_buf(request->rq_repmsg, 0);
-                md.obdo = obdo;
+                md.md = smd;
         } else {
                 request = it->it_data;
                 body = lustre_msg_buf(request->rq_repmsg, 1);
-                md.obdo = NULL;
+                md.md = NULL;
         }
 
-        body->valid = (__u32)OBD_MD_FLNOTOBD;
+        body->valid = OBD_MD_FLNOTOBD;
 
         body->nlink = 1;
         body->atime = body->ctime = body->mtime = time;
         body->uid = current->fsuid;
-        body->gid = current->fsgid;
+        body->gid = gid;
         body->mode = mode;
 
         md.body = body;
 
         inode = iget4(dir->i_sb, body->ino, ll_find_inode, &md);
         if (IS_ERR(inode)) {
-                CERROR("new_inode -fatal:  %ld\n", PTR_ERR(inode));
-                inode = ERR_PTR(-EIO);
+                rc = PTR_ERR(inode);
+                CERROR("new_inode -fatal: rc %d\n", rc);
                 LBUG();
-                GOTO(out, -EIO);
+                GOTO(out, rc);
         }
 
         if (!list_empty(&inode->i_dentry)) {
@@ -332,24 +357,25 @@ int ll_mdc_rename(struct inode *src, struct inode *tgt,
 
 static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
 {
-        int rc = 0;
+        int err, rc = 0;
         struct obdo oa;
         struct inode *inode;
+        struct lov_stripe_md *smd;
+        struct ll_inode_info *ii;
 
         if (dentry->d_it->it_disposition == 0) {
                 memset(&oa, 0, sizeof(oa));
                 oa.o_valid = OBD_MD_FLMODE;
                 oa.o_mode = S_IFREG | 0600;
-                rc = obd_create(ll_i2obdconn(dir), &oa);
+                rc = obd_create(ll_i2obdconn(dir), &oa, &smd);
                 if (rc)
                         RETURN(rc);
         }
 
-        mode = mode | S_IFREG;
         CDEBUG(D_DENTRY, "name %s mode %o o_id %lld\n",
                dentry->d_name.name, mode, (unsigned long long)oa.o_id);
         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
-                               NULL, 0, mode, 0, dentry->d_it, &oa);
+                               NULL, 0, mode, 0, dentry->d_it, smd);
 
         if (IS_ERR(inode)) {
                 rc = PTR_ERR(inode);
@@ -358,11 +384,6 @@ static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
                 GOTO(out_destroy, rc);
         }
 
-        // XXX clean up the object
-        inode->i_op = &ll_file_inode_operations;
-        inode->i_fop = &ll_file_operations;
-        inode->i_mapping->a_ops = &ll_aops;
-
         if (dentry->d_it->it_disposition) {
                 struct ll_inode_info *ii = ll_i2info(inode);
                 ii->lli_flags |= OBD_FL_CREATEONOPEN;
@@ -379,10 +400,11 @@ static int ll_create(struct inode * dir, struct dentry * dentry, int mode)
         RETURN(rc);
 
 out_destroy:
-        rc = obd_destroy(ll_i2obdconn(dir), &oa);
-        if (rc)
+        oa.o_easize = ii->lli_smd->lmd_size;
+        err = obd_destroy(ll_i2obdconn(dir), &oa, ii->lli_smd);
+        if (err)
                 CERROR("error destroying object %Ld in error path: err = %d\n",
-                       (unsigned long long)oa.o_id, rc);
+                       (unsigned long long)oa.o_id, err);
         return rc;
 }
 
@@ -391,12 +413,10 @@ static int ll_mknod(struct inode *dir, struct dentry *dentry, int mode,
 {
         struct inode * inode = ll_create_node(dir, dentry->d_name.name,
                                               dentry->d_name.len, NULL, 0,
-                                              mode, rdev, NULL, NULL);
+                                              mode, rdev, dentry->d_it, NULL);
         int err = PTR_ERR(inode);
-        if (!IS_ERR(inode)) {
-                init_special_inode(inode, mode, rdev);
+        if (!IS_ERR(inode))
                 err = ext2_add_nondir(dentry, inode);
-        }
         return err;
 }
 
@@ -408,9 +428,6 @@ static int ll_symlink(struct inode *dir, struct dentry *dentry,
         struct inode * inode;
         struct ll_inode_info *oinfo;
 
-        if (l > LL_INLINESZ)
-                return err;
-
         inode = ll_create_node(dir, dentry->d_name.name,
                                dentry->d_name.len, symname, l,
                                S_IFLNK | S_IRWXUGO, 0, dentry->d_it, NULL);
@@ -420,9 +437,9 @@ static int ll_symlink(struct inode *dir, struct dentry *dentry,
 
         oinfo = ll_i2info(inode);
 
-        inode->i_op = &ll_fast_symlink_inode_operations;
-        memcpy(oinfo->lli_inline, symname, l);
-        inode->i_size = l-1;
+        OBD_ALLOC(oinfo->lli_symlink_name, l + 1);
+        memcpy(oinfo->lli_symlink_name, symname, l + 1);
+        inode->i_size = l;
 
         err = ext2_add_nondir(dentry, inode);
 
@@ -477,9 +494,6 @@ static int ll_mkdir(struct inode * dir, struct dentry * dentry, int mode)
         if (IS_ERR(inode))
                 goto out_dir;
 
-        inode->i_op = &ll_dir_inode_operations;
-        inode->i_fop = &ll_dir_operations;
-        inode->i_mapping->a_ops = &ll_aops;
         inode->i_nlink = 1;
         ext2_inc_count(inode);
 
@@ -517,6 +531,11 @@ static int ll_unlink(struct inode * dir, struct dentry *dentry)
         struct page * page;
         int err = -ENOENT;
 
+        if (dentry->d_it && dentry->d_it->it_disposition) {
+                inode->i_nlink = 0;
+                GOTO(out, err = dentry->d_it->it_status);
+        }
+
         de = ext2_find_entry (dir, dentry, &page);
         if (!de)
                 goto out;
@@ -532,7 +551,6 @@ static int ll_unlink(struct inode * dir, struct dentry *dentry)
 
         inode->i_ctime = dir->i_ctime;
         ext2_dec_count(inode);
-        err = 0;
 out:
         return err;
 }
@@ -540,21 +558,26 @@ out:
 static int ll_rmdir(struct inode * dir, struct dentry *dentry)
 {
         struct inode * inode = dentry->d_inode;
-        int err = -ENOTEMPTY;
+        int err = 0;
+        int intent_did = dentry->d_it && dentry->d_it->it_disposition;
+
+        if (!intent_did) {
+                if (!ext2_empty_dir(inode))
+                LBUG();
 
-        if (ext2_empty_dir(inode)) {
                 err = ll_unlink(dir, dentry);
-                if (!err) {
-                        inode->i_size = 0;
-                        ext2_dec_count(inode);
-                        ext2_dec_count(dir);
-                }
-        }
-        return err;
+                if (err)
+                        RETURN(err);
+        } else
+                err = dentry->d_it->it_status;
+        inode->i_size = 0;
+        ext2_dec_count(inode);
+        ext2_dec_count(dir);
+        RETURN(err);
 }
 
-static int ll_rename (struct inode * old_dir, struct dentry * old_dentry,
-                      struct inode * new_dir, struct dentry * new_dentry )
+static int ll_rename(struct inode * old_dir, struct dentry * old_dentry,
+                     struct inode * new_dir, struct dentry * new_dentry)
 {
         struct inode * old_inode = old_dentry->d_inode;
         struct inode * new_inode = new_dentry->d_inode;
@@ -564,11 +587,8 @@ static int ll_rename (struct inode * old_dir, struct dentry * old_dentry,
         struct ext2_dir_entry_2 * old_de;
         int err = -ENOENT;
 
-        if (new_dentry->d_it) {
-                struct ptlrpc_request *req = new_dentry->d_it->it_data;
-                err = req->rq_status;
-                goto out;
-        }
+        if (new_dentry->d_it && new_dentry->d_it->it_disposition)
+                GOTO(out, err = new_dentry->d_it->it_status);
 
         err = ll_mdc_rename(old_dir, new_dir, old_dentry, new_dentry);
         if (err)