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