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