Whamcloud - gitweb
No new functionality outside of the DLM. ptlrpc_client no longer contains
[fs/lustre-release.git] / lustre / llite / file.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/ext2/file.c
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copyright (C) 1992, 1993, 1994, 1995
10  * Remy Card (card@masi.ibp.fr)
11  * Laboratoire MASI - Institut Blaise Pascal
12  * Universite Pierre et Marie Curie (Paris VI)
13  *
14  *  from
15  *
16  *  linux/fs/minix/file.c
17  *
18  *  Copyright (C) 1991, 1992  Linus Torvalds
19  *
20  *  ext2 fs regular file handling primitives
21  *
22  *  64-bit file support on 64-bit platforms by Jakub Jelinek
23  *      (jj@sunsite.ms.mff.cuni.cz)
24  */
25
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
28
29 #include <linux/errno.h>
30 #include <linux/fs.h>
31 #include <linux/fcntl.h>
32 #include <linux/sched.h>
33 #include <linux/stat.h>
34 #include <linux/locks.h>
35 #include <linux/mm.h>
36 #include <linux/pagemap.h>
37 #include <linux/smp_lock.h>
38
39 #define DEBUG_SUBSYSTEM S_LLITE
40
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lite.h>
43
44 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
45 extern int ll_setattr(struct dentry *de, struct iattr *attr);
46 extern inline struct obdo * ll_oa_from_inode(struct inode *inode, int valid);
47
48 static int ll_file_open(struct inode *inode, struct file *file)
49 {
50         int rc; 
51         struct ptlrpc_request *req;
52         struct ll_file_data *fd;
53         struct obdo *oa;
54         struct ll_sb_info *sbi = ll_i2sbi(inode);
55         ENTRY;
56
57         if (file->private_data) 
58                 LBUG();
59
60         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL); 
61         if (!fd)
62                 GOTO(out, rc = -ENOMEM);
63         memset(fd, 0, sizeof(*fd));
64
65         rc = mdc_open(&sbi->ll_mds_client, &sbi->ll_mds_peer, inode->i_ino,
66                       S_IFREG, file->f_flags, &fd->fd_mdshandle, &req); 
67         if (!fd->fd_mdshandle)
68                 CERROR("mdc_open didn't assign fd_mdshandle\n");
69
70         ptlrpc_free_req(req);
71         if (rc) {
72                 if (rc > 0) 
73                         rc = -rc;
74                 GOTO(out, rc);
75         }
76
77         oa = ll_oa_from_inode(inode, (OBD_MD_FLMODE | OBD_MD_FLID));
78         if (oa == NULL)
79                 LBUG();
80         rc = obd_open(ll_i2obdconn(inode), oa); 
81         obdo_free(oa);
82         if (rc) {
83                 /* XXX: Need to do mdc_close here! */
84                 if (rc > 0) 
85                         rc = -rc;
86                 GOTO(out, rc);
87         }
88
89         file->private_data = fd;
90
91         EXIT; 
92  out:
93         if (rc && fd) {
94                 kmem_cache_free(ll_file_data_slab, fd); 
95                 file->private_data = NULL;
96         }
97
98         return rc;
99 }
100
101 static int ll_file_release(struct inode *inode, struct file *file)
102 {
103         int rc;
104         struct ptlrpc_request *req;
105         struct ll_file_data *fd;
106         struct obdo *oa;
107         struct ll_sb_info *sbi = ll_i2sbi(inode);
108         struct iattr iattr;
109
110         ENTRY;
111
112         fd = (struct ll_file_data *)file->private_data;
113         if (!fd || !fd->fd_mdshandle) { 
114                 LBUG();
115                 GOTO(out, rc = -EINVAL);
116         }
117
118         oa = ll_oa_from_inode(inode, (OBD_MD_FLMODE | OBD_MD_FLID));
119         if (oa == NULL)
120                 LBUG();
121         rc = obd_close(ll_i2obdconn(inode), oa); 
122         obdo_free(oa);
123         if (rc) { 
124                 if (rc > 0) 
125                         rc = -rc;
126                 GOTO(out, rc);
127         }
128
129         iattr.ia_valid = ATTR_SIZE;
130         iattr.ia_size = inode->i_size;
131         rc = ll_inode_setattr(inode, &iattr, 0);
132         if (rc) {
133                 CERROR("failed - %d.\n", rc);
134                 rc = -EIO;
135         }
136
137         rc = mdc_close(&sbi->ll_mds_client, &sbi->ll_mds_peer, inode->i_ino,
138                        S_IFREG, fd->fd_mdshandle, &req); 
139         ptlrpc_free_req(req);
140         if (rc) { 
141                 if (rc > 0) 
142                         rc = -rc;
143                 GOTO(out, rc);
144         }
145         EXIT; 
146
147  out:
148         if (!rc && fd) { 
149                 kmem_cache_free(ll_file_data_slab, fd); 
150                 file->private_data = NULL;
151         }
152         return rc;
153 }
154
155
156 static inline void ll_remove_suid(struct inode *inode)
157 {
158         unsigned int mode;
159
160         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
161         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
162
163         /* was any of the uid bits set? */
164         mode &= inode->i_mode;
165         if (mode && !capable(CAP_FSETID)) {
166                 inode->i_mode &= ~mode;
167                 // XXX careful here - we cannot change the size
168         }
169 }
170
171
172 /*
173  * Write to a file (through the page cache).
174  */
175 static ssize_t
176 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
177 {
178         ssize_t retval;
179         CDEBUG(D_INFO, "Writing inode %ld, %d bytes, offset %Ld\n",
180                file->f_dentry->d_inode->i_ino, count, *ppos);
181
182         retval = generic_file_write(file, buf, count, ppos);
183         CDEBUG(D_INFO, "Wrote %d\n", retval);
184
185         /* update mtime/ctime/atime here, NOT size */
186         if (retval > 0) {
187                 struct iattr attr;
188                 attr.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_ATIME;
189                 attr.ia_mtime = attr.ia_ctime = attr.ia_atime =
190                         CURRENT_TIME;
191                 ll_setattr(file->f_dentry, &attr);
192         }
193         EXIT;
194         return retval;
195 }
196
197
198 /* XXX this does not need to do anything for data, it _does_ need to
199    call setattr */ 
200 int ll_fsync(struct file *file, struct dentry *dentry, int data)
201 {
202         return 0;
203 }
204
205 struct file_operations ll_file_operations = {
206         read: generic_file_read,
207         write: ll_file_write,
208         open: ll_file_open,
209         release: ll_file_release,
210         mmap: generic_file_mmap,
211         fsync: NULL
212 };
213
214
215 struct inode_operations ll_file_inode_operations = {
216         truncate: ll_truncate,
217         setattr: ll_setattr
218 };
219