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