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