Whamcloud - gitweb
liblustre:
[fs/lustre-release.git] / lustre / liblustre / file.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/queue.h>
32
33 #include <sysio.h>
34 #include <fs.h>
35 #include <mount.h>
36 #include <inode.h>
37 #include <file.h>
38
39 #undef LIST_HEAD
40
41 #include "llite_lib.h"
42
43 void llu_prepare_mdc_op_data(struct mdc_op_data *data,
44                              struct inode *i1,
45                              struct inode *i2,
46                              const char *name,
47                              int namelen,
48                              int mode)
49 {
50         LASSERT(i1);
51         
52         ll_i2uctxt(&data->ctxt, i1, i2);
53         ll_inode2fid(&data->fid1, i1);
54
55         if (i2) {
56                 ll_inode2fid(&data->fid2, i2);
57         }
58
59         data->name = name;
60         data->namelen = namelen;
61         data->create_mode = mode;
62         data->mod_time = CURRENT_TIME;
63 }
64
65 void obdo_refresh_inode(struct inode *dst,
66                         struct obdo *src,
67                         obd_flag valid)
68 {
69         struct llu_inode_info *lli = llu_i2info(dst);
70         valid &= src->o_valid;
71
72         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
73                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
74                        src->o_valid, LTIME_S(lli->lli_st_mtime), 
75                        LTIME_S(lli->lli_st_ctime),
76                        (long)src->o_mtime, (long)src->o_ctime);
77
78         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
79                 LTIME_S(lli->lli_st_atime) = src->o_atime;
80         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
81                 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
82         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
83                 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
84         if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
85                 lli->lli_st_size = src->o_size;
86         /* optimum IO size */
87         if (valid & OBD_MD_FLBLKSZ)
88                 lli->lli_st_blksize = src->o_blksize;
89         /* allocation of space */
90         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
91                 lli->lli_st_blocks = src->o_blocks;
92 }
93
94 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
95 {
96         struct ptlrpc_request *req = it->d.lustre.it_data;
97         struct ll_file_data *fd;
98         struct mds_body *body;
99         ENTRY;
100
101         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
102         LASSERT (body != NULL);                 /* reply already checked out */
103         LASSERT_REPSWABBED (req, 1);            /* and swabbed down */
104
105         /* already opened? */
106         if (lli->lli_open_count++)
107                 RETURN(0);
108                 
109         LASSERT(!lli->lli_file_data);
110
111         OBD_ALLOC(fd, sizeof(*fd));
112         /* We can't handle this well without reorganizing ll_file_open and
113          * ll_mdc_close, so don't even try right now. */
114         LASSERT(fd != NULL);
115
116         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
117         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
118         lli->lli_file_data = fd;
119
120         mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
121
122         RETURN(0);
123 }
124
125 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
126 {
127         struct inode *inode = pnode->p_base->pb_ino;
128         struct llu_inode_info *lli = llu_i2info(inode);
129         struct ll_file_data *fd;
130         struct ptlrpc_request *request;
131         struct lookup_intent *it;
132         struct lov_stripe_md *lsm;
133         int rc = 0;
134         ENTRY;
135
136         /* don't do anything for '/' */
137         if (llu_is_root_inode(inode))
138                 RETURN(0);
139
140         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
141         LL_GET_INTENT(inode, it);
142
143         if (!it->d.lustre.it_disposition) {
144                 LBUG();
145         }
146
147         rc = it_open_error(DISP_OPEN_OPEN, it);
148         if (rc)
149                 GOTO(out_release, rc);
150
151         rc = llu_local_open(lli, it);
152         if (rc)
153                 LBUG();
154
155         if (!S_ISREG(lli->lli_st_mode))
156                 GOTO(out_release, rc = 0);
157                 
158         fd = lli->lli_file_data;
159
160         lsm = lli->lli_smd;
161         if (lsm == NULL) {
162                 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
163                         CDEBUG(D_INODE, "object creation was delayed\n");
164                         GOTO(out_release, rc);
165                 }
166         }
167         fd->fd_flags &= ~O_LOV_DELAY_CREATE;
168
169  out_release:
170         request = it->d.lustre.it_data;
171         ptlrpc_req_finished(request);
172
173         it->it_op_release(it);
174         OBD_FREE(it, sizeof(*it));
175
176         RETURN(rc);
177 }
178
179 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
180 {
181         struct mds_body *body;
182         struct lov_mds_md *eadata;
183         struct lov_stripe_md *lsm = NULL;
184         struct obd_trans_info oti = { 0 };
185         struct obdo *oa;
186         int rc;
187         ENTRY;
188
189         /* req is swabbed so this is safe */
190         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
191
192         if (!(body->valid & OBD_MD_FLEASIZE))
193                 RETURN(0);
194
195         if (body->eadatasize == 0) {
196                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
197                 GOTO(out, rc = -EPROTO);
198         }
199
200         /* The MDS sent back the EA because we unlinked the last reference
201          * to this file. Use this EA to unlink the objects on the OST.
202          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
203          * check it is complete and sensible. */
204         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
205         LASSERT(eadata != NULL);
206         if (eadata == NULL) {
207                 CERROR("Can't unpack MDS EA data\n");
208                 GOTO(out, rc = -EPROTO);
209         }
210
211         rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
212         if (rc < 0) {
213                 CERROR("obd_unpackmd: %d\n", rc);
214                 GOTO(out, rc);
215         }
216         LASSERT(rc >= sizeof(*lsm));
217
218         oa = obdo_alloc();
219         if (oa == NULL)
220                 GOTO(out_free_memmd, rc = -ENOMEM);
221
222         oa->o_id = lsm->lsm_object_id;
223         oa->o_mode = body->mode & S_IFMT;
224         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
225
226         if (body->valid & OBD_MD_FLCOOKIE) {
227                 oa->o_valid |= OBD_MD_FLCOOKIE;
228                 oti.oti_logcookies =
229                         lustre_msg_buf(request->rq_repmsg, 2,
230                                        sizeof(struct llog_cookie) *
231                                        lsm->lsm_stripe_count);
232                 if (oti.oti_logcookies == NULL) {
233                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
234                         body->valid &= ~OBD_MD_FLCOOKIE;
235                 }
236         }
237
238         rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
239         obdo_free(oa);
240         if (rc)
241                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
242                        lsm->lsm_object_id, rc);
243  out_free_memmd:
244         obd_free_memmd(llu_i2obdexp(dir), &lsm);
245  out:
246         return rc;
247 }
248
249 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
250 {
251         struct llu_inode_info *lli = llu_i2info(inode);
252         struct ll_file_data *fd = lli->lli_file_data;
253         struct ptlrpc_request *req = NULL;
254         struct obd_client_handle *och = &fd->fd_mds_och;
255         struct obdo obdo;
256         int rc, valid;
257         ENTRY;
258
259         valid = OBD_MD_FLID;
260         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
261                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
262
263         memset(&obdo, 0, sizeof(obdo));
264         obdo.o_id = lli->lli_st_ino;
265         obdo.o_mode = lli->lli_st_mode;
266         obdo.o_size = lli->lli_st_size;
267         obdo.o_blocks = lli->lli_st_blocks;
268         if (0 /* ll_is_inode_dirty(inode) */) {
269                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
270                 valid |= OBD_MD_FLFLAGS;
271         }
272         obdo.o_valid = valid;
273         rc = mdc_close(mdc_exp, &obdo, och, &req);
274         if (rc == EAGAIN) {
275                 /* We are the last writer, so the MDS has instructed us to get
276                  * the file size and any write cookies, then close again. */
277                 //ll_queue_done_writing(inode);
278                 rc = 0;
279         } else if (rc) {
280                 CERROR("inode %lu close failed: rc = %d\n", lli->lli_st_ino, rc);
281         } else {
282                 rc = llu_objects_destroy(req, inode);
283                 if (rc)
284                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
285                                 lli->lli_st_ino, rc);
286         }
287
288         mdc_clear_open_replay_data(och);
289         ptlrpc_req_finished(req);
290         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
291         lli->lli_file_data = NULL;
292         OBD_FREE(fd, sizeof(*fd));
293
294         RETURN(rc);
295 }
296
297 int llu_file_release(struct inode *inode)
298 {
299         struct ll_file_data *fd;
300         struct llu_sb_info *sbi = llu_i2sbi(inode);
301         struct llu_inode_info *lli = llu_i2info(inode);
302         int rc = 0, rc2;
303
304         ENTRY;
305         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
306                lli->lli_st_generation);
307
308         if (llu_is_root_inode(inode))
309                 RETURN(0);
310
311         /* still opened by others? */
312         if (--lli->lli_open_count)
313                 RETURN(0);
314
315         fd = lli->lli_file_data;
316         if (!fd) /* no process opened the file after an mcreate */
317                 RETURN(0);
318
319         rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
320         if (rc2 && !rc)
321                 rc = rc2;
322
323         RETURN(rc);
324 }
325
326 int llu_iop_close(struct inode *inode)
327 {
328         int rc;
329
330         rc = llu_file_release(inode);
331         /* if open count == 0 && stale_flag is set, should we
332          * remove the inode immediately? */
333         return rc;
334 }
335
336 int llu_iop_ipreadv(struct inode *ino,
337                     struct ioctx *ioctx)
338 {
339         ENTRY;
340
341         if (!ioctx->ioctx_iovlen)
342                 RETURN(0);
343         if (ioctx->ioctx_iovlen < 0)
344                 RETURN(-EINVAL);
345
346         ioctx->ioctx_private = llu_file_read(ino,
347                                         ioctx->ioctx_iovec,
348                                         ioctx->ioctx_iovlen,
349                                         ioctx->ioctx_offset);
350         if (IS_ERR(ioctx->ioctx_private))
351                 return (PTR_ERR(ioctx->ioctx_private));
352
353         RETURN(0);
354 }
355
356 int llu_iop_ipwritev(struct inode *ino,
357                      struct ioctx *ioctx)
358 {
359         ENTRY;
360
361         if (!ioctx->ioctx_iovlen)
362                 RETURN(0);
363         if (ioctx->ioctx_iovlen < 0)
364                 RETURN(-EINVAL);
365
366         ioctx->ioctx_private = llu_file_write(ino,
367                                          ioctx->ioctx_iovec,
368                                          ioctx->ioctx_iovlen,
369                                          ioctx->ioctx_offset);
370         if (IS_ERR(ioctx->ioctx_private))
371                 return (PTR_ERR(ioctx->ioctx_private));
372
373         RETURN(0);
374 }
375
376 /* this isn't where truncate starts.   roughly:
377  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
378  * we grab the lock back in setattr_raw to avoid races. */
379 static void llu_truncate(struct inode *inode)
380 {
381         struct llu_inode_info *lli = llu_i2info(inode);
382         struct lov_stripe_md *lsm = lli->lli_smd;
383         struct obdo oa = {0};
384         int err;
385         ENTRY;
386         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
387                lli->lli_st_generation);
388
389         if (!lsm) {
390                 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
391                 EXIT;
392                 return;
393         }
394
395         oa.o_id = lsm->lsm_object_id;
396         oa.o_valid = OBD_MD_FLID;
397         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
398                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
399
400         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
401                oa.o_id, lli->lli_st_size);
402
403         /* truncate == punch from new size to absolute end of file */
404         err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
405                         OBD_OBJECT_EOF, NULL);
406         if (err)
407                 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
408         else
409                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
410                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
411                                           OBD_MD_FLCTIME);
412
413         EXIT;
414         return;
415 }
416
417 int llu_vmtruncate(struct inode * inode, loff_t offset)
418 {
419         struct llu_inode_info *lli = llu_i2info(inode);
420
421         lli->lli_st_size = offset;
422
423         llu_truncate(inode);
424
425         return 0;
426 }