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