Whamcloud - gitweb
9b645d08b32d5c0cd9d8f97f226dc822817e5688
[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         lli->lli_open_flags = flags;
170
171  out_release:
172         request = it->d.lustre.it_data;
173         ptlrpc_req_finished(request);
174
175         it->it_op_release(it);
176         OBD_FREE(it, sizeof(*it));
177
178         RETURN(rc);
179 }
180
181 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
182 {
183         struct mds_body *body;
184         struct lov_mds_md *eadata;
185         struct lov_stripe_md *lsm = NULL;
186         struct obd_trans_info oti = { 0 };
187         struct obdo *oa;
188         int rc;
189         ENTRY;
190
191         /* req is swabbed so this is safe */
192         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
193
194         if (!(body->valid & OBD_MD_FLEASIZE))
195                 RETURN(0);
196
197         if (body->eadatasize == 0) {
198                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
199                 GOTO(out, rc = -EPROTO);
200         }
201
202         /* The MDS sent back the EA because we unlinked the last reference
203          * to this file. Use this EA to unlink the objects on the OST.
204          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
205          * check it is complete and sensible. */
206         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
207         LASSERT(eadata != NULL);
208         if (eadata == NULL) {
209                 CERROR("Can't unpack MDS EA data\n");
210                 GOTO(out, rc = -EPROTO);
211         }
212
213         rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
214         if (rc < 0) {
215                 CERROR("obd_unpackmd: %d\n", rc);
216                 GOTO(out, rc);
217         }
218         LASSERT(rc >= sizeof(*lsm));
219
220         oa = obdo_alloc();
221         if (oa == NULL)
222                 GOTO(out_free_memmd, rc = -ENOMEM);
223
224         oa->o_id = lsm->lsm_object_id;
225         oa->o_mode = body->mode & S_IFMT;
226         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
227
228         if (body->valid & OBD_MD_FLCOOKIE) {
229                 oa->o_valid |= OBD_MD_FLCOOKIE;
230                 oti.oti_logcookies =
231                         lustre_msg_buf(request->rq_repmsg, 2,
232                                        sizeof(struct llog_cookie) *
233                                        lsm->lsm_stripe_count);
234                 if (oti.oti_logcookies == NULL) {
235                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
236                         body->valid &= ~OBD_MD_FLCOOKIE;
237                 }
238         }
239
240         rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
241         obdo_free(oa);
242         if (rc)
243                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
244                        lsm->lsm_object_id, rc);
245  out_free_memmd:
246         obd_free_memmd(llu_i2obdexp(dir), &lsm);
247  out:
248         return rc;
249 }
250
251 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
252 {
253         struct llu_inode_info *lli = llu_i2info(inode);
254         struct ll_file_data *fd = lli->lli_file_data;
255         struct ptlrpc_request *req = NULL;
256         struct obd_client_handle *och = &fd->fd_mds_och;
257         struct obdo obdo;
258         int rc, valid;
259         ENTRY;
260
261         /* clear group lock, if present */
262         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
263                 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
264                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
265                 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
266                                        &fd->fd_cwlockh);
267         }
268
269         valid = OBD_MD_FLID;
270         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
271                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
272
273         memset(&obdo, 0, sizeof(obdo));
274         obdo.o_id = lli->lli_st_ino;
275         obdo.o_mode = lli->lli_st_mode;
276         obdo.o_size = lli->lli_st_size;
277         obdo.o_blocks = lli->lli_st_blocks;
278         if (0 /* ll_is_inode_dirty(inode) */) {
279                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
280                 valid |= OBD_MD_FLFLAGS;
281         }
282         obdo.o_valid = valid;
283         rc = mdc_close(mdc_exp, &obdo, och, &req);
284         if (rc == EAGAIN) {
285                 /* We are the last writer, so the MDS has instructed us to get
286                  * the file size and any write cookies, then close again. */
287                 //ll_queue_done_writing(inode);
288                 rc = 0;
289         } else if (rc) {
290                 CERROR("inode %lu close failed: rc = %d\n", lli->lli_st_ino, rc);
291         } else {
292                 rc = llu_objects_destroy(req, inode);
293                 if (rc)
294                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
295                                 lli->lli_st_ino, rc);
296         }
297
298         mdc_clear_open_replay_data(och);
299         ptlrpc_req_finished(req);
300         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
301         lli->lli_file_data = NULL;
302         OBD_FREE(fd, sizeof(*fd));
303
304         RETURN(rc);
305 }
306
307 int llu_file_release(struct inode *inode)
308 {
309         struct ll_file_data *fd;
310         struct llu_sb_info *sbi = llu_i2sbi(inode);
311         struct llu_inode_info *lli = llu_i2info(inode);
312         int rc = 0, rc2;
313
314         ENTRY;
315         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
316                lli->lli_st_generation);
317
318         if (llu_is_root_inode(inode))
319                 RETURN(0);
320
321         /* still opened by others? */
322         if (--lli->lli_open_count)
323                 RETURN(0);
324
325         fd = lli->lli_file_data;
326         if (!fd) /* no process opened the file after an mcreate */
327                 RETURN(0);
328
329         rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
330         if (rc2 && !rc)
331                 rc = rc2;
332
333         RETURN(rc);
334 }
335
336 int llu_iop_close(struct inode *inode)
337 {
338         int rc;
339
340         rc = llu_file_release(inode);
341         /* if open count == 0 && stale_flag is set, should we
342          * remove the inode immediately? */
343         return rc;
344 }
345
346 int llu_iop_ipreadv(struct inode *ino,
347                     struct ioctx *ioctx)
348 {
349         ENTRY;
350
351         if (!ioctx->ioctx_iovlen)
352                 RETURN(0);
353         if (ioctx->ioctx_iovlen < 0)
354                 RETURN(-EINVAL);
355
356         ioctx->ioctx_private = llu_file_read(ino,
357                                         ioctx->ioctx_iovec,
358                                         ioctx->ioctx_iovlen,
359                                         ioctx->ioctx_offset);
360         if (IS_ERR(ioctx->ioctx_private))
361                 return (PTR_ERR(ioctx->ioctx_private));
362
363         RETURN(0);
364 }
365
366 int llu_iop_ipwritev(struct inode *ino,
367                      struct ioctx *ioctx)
368 {
369         ENTRY;
370
371         if (!ioctx->ioctx_iovlen)
372                 RETURN(0);
373         if (ioctx->ioctx_iovlen < 0)
374                 RETURN(-EINVAL);
375
376         ioctx->ioctx_private = llu_file_write(ino,
377                                          ioctx->ioctx_iovec,
378                                          ioctx->ioctx_iovlen,
379                                          ioctx->ioctx_offset);
380         if (IS_ERR(ioctx->ioctx_private))
381                 return (PTR_ERR(ioctx->ioctx_private));
382
383         RETURN(0);
384 }
385
386 /* this isn't where truncate starts.   roughly:
387  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
388  * we grab the lock back in setattr_raw to avoid races. */
389 static void llu_truncate(struct inode *inode)
390 {
391         struct llu_inode_info *lli = llu_i2info(inode);
392         struct lov_stripe_md *lsm = lli->lli_smd;
393         struct obdo oa = {0};
394         int err;
395         ENTRY;
396         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
397                lli->lli_st_generation);
398
399         if (!lsm) {
400                 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
401                 EXIT;
402                 return;
403         }
404
405         oa.o_id = lsm->lsm_object_id;
406         oa.o_valid = OBD_MD_FLID;
407         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
408                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
409
410         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
411                oa.o_id, lli->lli_st_size);
412
413         /* truncate == punch from new size to absolute end of file */
414         err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
415                         OBD_OBJECT_EOF, NULL);
416         if (err)
417                 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
418         else
419                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
420                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
421                                           OBD_MD_FLCTIME);
422
423         EXIT;
424         return;
425 }
426
427 int llu_vmtruncate(struct inode * inode, loff_t offset)
428 {
429         struct llu_inode_info *lli = llu_i2info(inode);
430
431         lli->lli_st_size = offset;
432
433         llu_truncate(inode);
434
435         return 0;
436 }