Whamcloud - gitweb
cf5267bac989a321739151863750817d0399be4f
[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 file operations
5  *
6  *  Copyright (c) 2002-2004 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/stat.h>
32 #include <sys/queue.h>
33 #include <fcntl.h>
34
35 #include <sysio.h>
36 #ifdef HAVE_XTIO_H
37 #include <xtio.h>
38 #endif
39 #include <fs.h>
40 #include <mount.h>
41 #include <inode.h>
42 #ifdef HAVE_FILE_H
43 #include <file.h>
44 #endif
45
46 #undef LIST_HEAD
47
48 #include "llite_lib.h"
49
50 /* Pack the required supplementary groups into the supplied groups array.
51  * If we don't need to use the groups from the target inode(s) then we
52  * instead pack one or more groups from the user's supplementary group
53  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
54 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
55 {
56         LASSERT(i1 != NULL);
57         LASSERT(suppgids != NULL);
58
59         if (in_group_p(i1->i_stbuf.st_gid))
60                 suppgids[0] = i1->i_stbuf.st_gid;
61         else
62                 suppgids[0] = -1;
63
64         if (i2) {
65                 if (in_group_p(i2->i_stbuf.st_gid))
66                         suppgids[1] = i2->i_stbuf.st_gid;
67                 else
68                         suppgids[1] = -1;
69         } else {
70                 suppgids[1] = -1;
71         }
72 }
73
74 void llu_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
75                          struct inode *i2, const char *name, int namelen,
76                          int mode, __u32 opc)
77 {
78         LASSERT(i1 != NULL || i2 != NULL);
79         LASSERT(op_data);
80
81         if (i1) {
82                 ll_i2gids(op_data->op_suppgids, i1, i2);
83                 op_data->op_fid1 = *ll_inode2fid(i1);
84         }else {
85                 ll_i2gids(op_data->op_suppgids, i2, i1);
86                 op_data->op_fid1 = *ll_inode2fid(i2);
87         }
88
89         if (i2)
90                 op_data->op_fid2 = *ll_inode2fid(i2);
91         else
92                 fid_zero(&op_data->op_fid2);
93
94         op_data->op_opc = opc;
95         op_data->op_name = name;
96         op_data->op_mode = mode;
97         op_data->op_namelen = namelen;
98         op_data->op_mod_time = CURRENT_TIME;
99         op_data->op_data = NULL;
100 }
101
102 void llu_finish_md_op_data(struct md_op_data *op_data)
103 {
104         OBD_FREE_PTR(op_data);
105 }
106
107 void obdo_refresh_inode(struct inode *dst,
108                         struct obdo *src,
109                         obd_flag valid)
110 {
111         struct intnl_stat *st = llu_i2stat(dst);
112         valid &= src->o_valid;
113
114         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
115                 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
116                        src->o_valid, LTIME_S(st->st_mtime),
117                        LTIME_S(st->st_ctime),
118                        (long)src->o_mtime, (long)src->o_ctime);
119
120         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(st->st_atime))
121                 LTIME_S(st->st_atime) = src->o_atime;
122         
123         /* mtime is always updated with ctime, but can be set in past.
124            As write and utime(2) may happen within 1 second, and utime's
125            mtime has a priority over write's one, leave mtime from mds 
126            for the same ctimes. */
127         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime)) {
128                 LTIME_S(st->st_ctime) = src->o_ctime;
129                 if (valid & OBD_MD_FLMTIME)
130                         LTIME_S(st->st_mtime) = src->o_mtime;
131         }
132         if (valid & OBD_MD_FLSIZE && src->o_size > st->st_size)
133                 st->st_size = src->o_size;
134         /* optimum IO size */
135         if (valid & OBD_MD_FLBLKSZ)
136                 st->st_blksize = src->o_blksize;
137         /* allocation of space */
138         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > st->st_blocks)
139                 st->st_blocks = src->o_blocks;
140 }
141
142 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
143 {
144         struct ptlrpc_request *req = it->d.lustre.it_data;
145         struct ll_file_data *fd;
146         struct mdt_body *body;
147         ENTRY;
148
149         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
150         LASSERT(body != NULL);                 /* reply already checked out */
151         /* and swabbed down */
152         LASSERT(lustre_rep_swabbed(req, DLM_REPLY_REC_OFF));
153
154         /* already opened? */
155         if (lli->lli_open_count++)
156                 RETURN(0);
157
158         LASSERT(!lli->lli_file_data);
159
160         OBD_ALLOC(fd, sizeof(*fd));
161         /* We can't handle this well without reorganizing ll_file_open and
162          * ll_md_close, so don't even try right now. */
163         LASSERT(fd != NULL);
164
165         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
166         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
167         fd->fd_mds_och.och_fid   = lli->lli_fid;
168         lli->lli_file_data = fd;
169
170         md_set_open_replay_data(lli->lli_sbi->ll_md_exp,
171                                 &fd->fd_mds_och, it->d.lustre.it_data);
172
173         RETURN(0);
174 }
175
176 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
177 {
178         struct inode *inode = pnode->p_base->pb_ino;
179         struct llu_inode_info *lli = llu_i2info(inode);
180         struct intnl_stat *st = llu_i2stat(inode);
181         struct ll_file_data *fd;
182         struct ptlrpc_request *request;
183         struct lookup_intent *it;
184         struct lov_stripe_md *lsm;
185         int rc = 0;
186         ENTRY;
187
188         liblustre_wait_event(0);
189
190         /* don't do anything for '/' */
191         if (llu_is_root_inode(inode))
192                 RETURN(0);
193
194         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
195         LL_GET_INTENT(inode, it);
196
197         if (!it->d.lustre.it_disposition) {
198                 LBUG();
199         }
200
201         rc = it_open_error(DISP_OPEN_OPEN, it);
202         if (rc)
203                 GOTO(out_release, rc);
204
205         rc = llu_local_open(lli, it);
206         if (rc)
207                 LBUG();
208
209         if (!S_ISREG(st->st_mode))
210                 GOTO(out_release, rc = 0);
211
212         fd = lli->lli_file_data;
213
214         lsm = lli->lli_smd;
215         if (lsm == NULL) {
216                 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
217                         CDEBUG(D_INODE, "object creation was delayed\n");
218                         GOTO(out_release, rc);
219                 }
220         }
221         fd->fd_flags &= ~O_LOV_DELAY_CREATE;
222
223         lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
224
225  out_release:
226         request = it->d.lustre.it_data;
227         ptlrpc_req_finished(request);
228
229         it->it_op_release(it);
230         OBD_FREE(it, sizeof(*it));
231
232         /* libsysio hasn't done anything for O_TRUNC. here we
233          * simply simulate it as open(...); truncate(...); */
234         if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
235                 struct iattr attr;
236
237                 memset(&attr, 0, sizeof(attr));
238                 attr.ia_size = 0;
239                 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
240                 rc = llu_setattr_raw(inode, &attr);
241                 if (rc)
242                         CERROR("error %d truncate in open()\n", rc);
243         }
244
245         liblustre_wait_event(0);
246         RETURN(rc);
247 }
248
249 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
250 {
251         struct mdt_body *body;
252         struct lov_mds_md *eadata;
253         struct lov_stripe_md *lsm = NULL;
254         struct obd_trans_info oti = { 0 };
255         struct obdo *oa;
256         int rc;
257         ENTRY;
258
259         /* req is swabbed so this is safe */
260         body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
261
262         if (!(body->valid & OBD_MD_FLEASIZE))
263                 RETURN(0);
264
265         if (body->eadatasize == 0) {
266                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
267                 GOTO(out, rc = -EPROTO);
268         }
269
270         /* The MDS sent back the EA because we unlinked the last reference
271          * to this file. Use this EA to unlink the objects on the OST.
272          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
273          * check it is complete and sensible. */
274         eadata = lustre_swab_repbuf(request, REPLY_REC_OFF+1, body->eadatasize,
275                                     NULL);
276         LASSERT(eadata != NULL);
277         if (eadata == NULL) {
278                 CERROR("Can't unpack MDS EA data\n");
279                 GOTO(out, rc = -EPROTO);
280         }
281
282         rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
283         if (rc < 0) {
284                 CERROR("obd_unpackmd: %d\n", rc);
285                 GOTO(out, rc);
286         }
287         LASSERT(rc >= sizeof(*lsm));
288
289         OBDO_ALLOC(oa);
290         if (oa == NULL)
291                 GOTO(out_free_memmd, rc = -ENOMEM);
292
293         oa->o_id = lsm->lsm_object_id;
294         oa->o_gr = lsm->lsm_object_gr;
295         oa->o_mode = body->mode & S_IFMT;
296         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
297
298         if (body->valid & OBD_MD_FLCOOKIE) {
299                 oa->o_valid |= OBD_MD_FLCOOKIE;
300                 oti.oti_logcookies =
301                         lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
302                                        sizeof(struct llog_cookie) *
303                                        lsm->lsm_stripe_count);
304                 if (oti.oti_logcookies == NULL) {
305                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
306                         body->valid &= ~OBD_MD_FLCOOKIE;
307                 }
308         }
309
310         rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL);
311         OBDO_FREE(oa);
312         if (rc)
313                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
314                        lsm->lsm_object_id, rc);
315  out_free_memmd:
316         obd_free_memmd(llu_i2obdexp(dir), &lsm);
317  out:
318         return rc;
319 }
320
321 int llu_sizeonmds_update(struct inode *inode, struct md_open_data *mod,
322                          struct lustre_handle *fh, __u64 ioepoch)
323 {
324         struct llu_inode_info *lli = llu_i2info(inode);
325         struct llu_sb_info *sbi = llu_i2sbi(inode);
326         struct md_op_data op_data = {{ 0 }};
327         struct obdo oa;
328         int rc;
329         ENTRY;
330         
331         LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
332         LASSERT(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM);
333         
334         rc = llu_inode_getattr(inode, &oa);
335         if (rc == -ENOENT) {
336                 oa.o_valid = 0;
337                 CDEBUG(D_INODE, "objid "LPX64" is already destroyed\n",
338                        lli->lli_smd->lsm_object_id);
339         } else if (rc) {
340                 CERROR("inode_getattr failed (%d): unable to send a "
341                        "Size-on-MDS attribute update for inode %llu/%lu\n",
342                        rc, (long long)llu_i2stat(inode)->st_ino,
343                        lli->lli_st_generation);
344                 RETURN(rc);
345         }
346         
347         md_from_obdo(&op_data, &oa, oa.o_valid);
348         memcpy(&op_data.op_handle, fh, sizeof(*fh));
349         op_data.op_ioepoch = ioepoch;
350         op_data.op_flags |= MF_SOM_CHANGE;
351
352         rc = llu_md_setattr(inode, &op_data, &mod);
353         RETURN(rc);
354 }
355
356 int llu_md_close(struct obd_export *md_exp, struct inode *inode)
357 {
358         struct llu_inode_info *lli = llu_i2info(inode);
359         struct ll_file_data *fd = lli->lli_file_data;
360         struct ptlrpc_request *req = NULL;
361         struct obd_client_handle *och = &fd->fd_mds_och;
362         struct intnl_stat *st = llu_i2stat(inode);
363         struct md_op_data op_data = { { 0 } };
364         int seq_end = 0, rc;
365         ENTRY;
366
367         /* clear group lock, if present */
368         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
369                 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
370                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
371                 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
372                                        &fd->fd_cwlockh);
373         }
374
375         op_data.op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
376                                 ATTR_MTIME_SET | ATTR_CTIME_SET;
377         
378         if (fd->fd_flags & FMODE_WRITE) {
379                 struct llu_sb_info *sbi = llu_i2sbi(inode);
380                 if (!(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM) ||
381                     !S_ISREG(llu_i2stat(inode)->st_mode)) {
382                         op_data.op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
383                 } else {
384                         /* Inode cannot be dirty. Close the epoch. */
385                         op_data.op_flags |= MF_EPOCH_CLOSE;
386                         /* XXX: Send CHANGE flag only if Size-on-MDS inode attributes
387                          * are really changed.  */
388                         op_data.op_flags |= MF_SOM_CHANGE;
389
390                         /* Pack Size-on-MDS attributes if we are in IO epoch and 
391                          * attributes are valid. */
392                         LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
393                         if (!llu_local_size(inode))
394                                 op_data.op_attr.ia_valid |= 
395                                         OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
396                 }
397         }
398         op_data.op_fid1 = lli->lli_fid;
399         op_data.op_attr.ia_atime = st->st_atime;
400         op_data.op_attr.ia_mtime = st->st_mtime;
401         op_data.op_attr.ia_ctime = st->st_ctime;
402         op_data.op_attr.ia_size = st->st_size;
403         op_data.op_attr_blocks = st->st_blocks;
404         op_data.op_attr.ia_attr_flags = lli->lli_st_flags;
405         op_data.op_ioepoch = lli->lli_ioepoch;
406         memcpy(&op_data.op_handle, &och->och_fh, sizeof(op_data.op_handle));
407
408         rc = md_close(md_exp, &op_data, och->och_mod, &req);
409         if (rc != -EAGAIN)
410                 seq_end = 1;
411
412         if (rc == -EAGAIN) {
413                 /* We are the last writer, so the MDS has instructed us to get
414                  * the file size and any write cookies, then close again. */
415                 LASSERT(fd->fd_flags & FMODE_WRITE);
416                 rc = llu_sizeonmds_update(inode, och->och_mod, &och->och_fh,
417                                           op_data.op_ioepoch);
418                 if (rc) {
419                         CERROR("inode %llu mdc Size-on-MDS update failed: "
420                                "rc = %d\n", (long long)st->st_ino, rc);
421                         rc = 0;
422                 }
423         } else if (rc) {
424                 CERROR("inode %llu close failed: rc %d\n",
425                        (long long)st->st_ino, rc);
426         } else {
427                 rc = llu_objects_destroy(req, inode);
428                 if (rc)
429                         CERROR("inode %llu ll_objects destroy: rc = %d\n",
430                                (long long)st->st_ino, rc);
431         }
432
433         if (seq_end)
434                 ptlrpc_close_replay_seq(req);
435         md_clear_open_replay_data(md_exp, och);
436         ptlrpc_req_finished(req);
437         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
438         lli->lli_file_data = NULL;
439         OBD_FREE(fd, sizeof(*fd));
440
441         RETURN(rc);
442 }
443
444 int llu_file_release(struct inode *inode)
445 {
446         struct ll_file_data *fd;
447         struct llu_sb_info *sbi = llu_i2sbi(inode);
448         struct llu_inode_info *lli = llu_i2info(inode);
449         int rc = 0, rc2;
450
451         ENTRY;
452         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
453                (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
454
455         if (llu_is_root_inode(inode))
456                 RETURN(0);
457
458         /* still opened by others? */
459         if (--lli->lli_open_count)
460                 RETURN(0);
461
462         fd = lli->lli_file_data;
463         if (!fd) /* no process opened the file after an mcreate */
464                 RETURN(0);
465
466         rc2 = llu_md_close(sbi->ll_md_exp, inode);
467         if (rc2 && !rc)
468                 rc = rc2;
469
470         RETURN(rc);
471 }
472
473 /*
474  * libsysio require us return 0
475  */
476 int llu_iop_close(struct inode *inode)
477 {
478         int rc;
479
480         liblustre_wait_event(0);
481
482         rc = llu_file_release(inode);
483         if (rc) {
484                 CERROR("file close error %d\n", rc);
485         }
486         /* if open count == 0 && stale_flag is set, should we
487          * remove the inode immediately? */
488         liblustre_wait_event(0);
489         return 0;
490 }
491
492 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
493 {
494         ENTRY;
495
496         liblustre_wait_event(0);
497
498         if (off < 0 || off > ll_file_maxbytes(ino))
499                 RETURN(-EINVAL);
500
501         RETURN(off);
502 }
503
504 /* this isn't where truncate starts.  roughly:
505  * llu_iop_{open,setattr}->llu_setattr_raw->llu_vmtruncate->llu_truncate
506  * we grab the lock back in setattr_raw to avoid races. */
507 static void llu_truncate(struct inode *inode, obd_flag flags)
508 {
509         struct llu_inode_info *lli = llu_i2info(inode);
510         struct intnl_stat *st = llu_i2stat(inode);
511         struct obd_info oinfo = { { { 0 } } };
512         struct obdo oa = { 0 };
513         int rc;
514         ENTRY;
515         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p) to %llu\n",
516                (long long)st->st_ino, lli->lli_st_generation, inode,
517                (long long)st->st_size);
518
519         if (!lli->lli_smd) {
520                 CDEBUG(D_INODE, "truncate on inode %llu with no objects\n",
521                        (long long)st->st_ino);
522                 EXIT;
523                 return;
524         }
525
526         oinfo.oi_md = lli->lli_smd;
527         oinfo.oi_policy.l_extent.start = st->st_size;
528         oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
529         oinfo.oi_oa = &oa;
530         oa.o_id = lli->lli_smd->lsm_object_id;
531         oa.o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
532         oa.o_flags = flags; /* We don't actually want to copy inode flags */
533  
534         obdo_from_inode(&oa, inode,
535                         OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLATIME |
536                         OBD_MD_FLMTIME | OBD_MD_FLCTIME);
537
538         obd_adjust_kms(llu_i2obdexp(inode), lli->lli_smd, st->st_size, 1);
539
540         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
541                oa.o_id, (long long)st->st_size);
542
543         /* truncate == punch from new size to absolute end of file */
544         rc = obd_punch_rqset(llu_i2obdexp(inode), &oinfo, NULL);
545         if (rc)
546                 CERROR("obd_truncate fails (%d) ino %llu\n",
547                        rc, (long long)st->st_ino);
548         else
549                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
550                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
551                                           OBD_MD_FLCTIME);
552
553         EXIT;
554         return;
555 } /* llu_truncate */
556
557 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag flags)
558 {
559         llu_i2stat(inode)->st_size = offset;
560
561         /*
562          * llu_truncate() is only called from this
563          * point. llu_vmtruncate/llu_truncate split exists to mimic the
564          * structure of Linux VFS truncate code path.
565          */
566
567         llu_truncate(inode, flags);
568
569         return 0;
570 }