Whamcloud - gitweb
b=21592 BLKGETSIZE64 redefinition on some platform.
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/liblustre/file.c
37  *
38  * Lustre Light file operations
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include <assert.h>
46 #include <time.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/queue.h>
50 #include <fcntl.h>
51
52 #include <sysio.h>
53 #ifdef HAVE_XTIO_H
54 #include <xtio.h>
55 #endif
56 #include <fs.h>
57 #include <mount.h>
58 #include <inode.h>
59 #ifdef HAVE_FILE_H
60 #include <file.h>
61 #endif
62
63 #undef LIST_HEAD
64
65 #include "llite_lib.h"
66
67 /* Pack the required supplementary groups into the supplied groups array.
68  * If we don't need to use the groups from the target inode(s) then we
69  * instead pack one or more groups from the user's supplementary group
70  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
71 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
72 {
73         LASSERT(i1 != NULL);
74         LASSERT(suppgids != NULL);
75
76         if (in_group_p(i1->i_stbuf.st_gid))
77                 suppgids[0] = i1->i_stbuf.st_gid;
78         else
79                 suppgids[0] = -1;
80
81         if (i2) {
82                 if (in_group_p(i2->i_stbuf.st_gid))
83                         suppgids[1] = i2->i_stbuf.st_gid;
84                 else
85                         suppgids[1] = -1;
86         } else {
87                 suppgids[1] = -1;
88         }
89 }
90
91 void llu_prepare_mdc_op_data(struct mdc_op_data *data,
92                              struct inode *i1,
93                              struct inode *i2,
94                              const char *name,
95                              int namelen,
96                              int mode)
97 {
98         LASSERT(i1 != NULL || i2 != NULL);
99
100         if (i1) {
101                 ll_i2gids(data->suppgids, i1, i2);
102                 llu_inode2fid(&data->fid1, i1);
103         }else {
104                 ll_i2gids(data->suppgids, i2, i1);
105                 llu_inode2fid(&data->fid1, i2);
106         }
107
108         if (i2)
109                 llu_inode2fid(&data->fid2, i2);
110         else
111                 memset(&data->fid2, 0, sizeof(data->fid2));
112
113         data->name = name;
114         data->namelen = namelen;
115         data->create_mode = mode;
116         data->mod_time = CURRENT_TIME;
117         data->data = NULL;
118 }
119
120 void obdo_refresh_inode(struct inode *dst,
121                         struct obdo *src,
122                         obd_flag valid)
123 {
124         struct intnl_stat *st = llu_i2stat(dst);
125         valid &= src->o_valid;
126
127         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
128                 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
129                        src->o_valid, LTIME_S(st->st_mtime),
130                        LTIME_S(st->st_ctime),
131                        (long)src->o_mtime, (long)src->o_ctime);
132
133         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(st->st_atime))
134                 LTIME_S(st->st_atime) = src->o_atime;
135         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(st->st_mtime))
136                 LTIME_S(st->st_mtime) = src->o_mtime;
137         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
138                 LTIME_S(st->st_ctime) = src->o_ctime;
139         if (valid & OBD_MD_FLSIZE && src->o_size > st->st_size)
140                 st->st_size = src->o_size;
141         /* optimum IO size */
142         if (valid & OBD_MD_FLBLKSZ)
143                 st->st_blksize = src->o_blksize;
144         /* allocation of space */
145         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > st->st_blocks)
146                 st->st_blocks = src->o_blocks;
147 }
148
149 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
150 {
151         struct ptlrpc_request *req = it->d.lustre.it_data;
152         struct ll_file_data *fd;
153         struct mds_body *body;
154         ENTRY;
155
156         body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF, sizeof(*body));
157         LASSERT(body != NULL);                 /* reply already checked out */
158         /* and swabbed down */
159         LASSERT(lustre_rep_swabbed(req, DLM_REPLY_REC_OFF));
160
161         /* already opened? */
162         if (lli->lli_open_count++)
163                 RETURN(0);
164
165         LASSERT(!lli->lli_file_data);
166
167         OBD_ALLOC(fd, sizeof(*fd));
168         /* We can't handle this well without reorganizing ll_file_open and
169          * ll_mdc_close, so don't even try right now. */
170         LASSERT(fd != NULL);
171
172         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
173         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
174         lli->lli_file_data = fd;
175
176         mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
177
178         RETURN(0);
179 }
180
181 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
182 {
183         struct inode *inode = pnode->p_base->pb_ino;
184         struct llu_inode_info *lli = llu_i2info(inode);
185         struct intnl_stat *st = llu_i2stat(inode);
186         struct ll_file_data *fd;
187         struct ptlrpc_request *request;
188         struct lookup_intent *it;
189         struct lov_stripe_md *lsm;
190         int rc = 0;
191         ENTRY;
192
193         liblustre_wait_event(0);
194
195         /* don't do anything for '/' */
196         if (llu_is_root_inode(inode))
197                 RETURN(0);
198
199         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
200         LL_GET_INTENT(inode, it);
201
202         if (!it->d.lustre.it_disposition) {
203                 LBUG();
204         }
205
206         rc = it_open_error(DISP_OPEN_OPEN, it);
207         if (rc)
208                 GOTO(out_release, rc);
209
210         rc = llu_local_open(lli, it);
211         if (rc)
212                 LBUG();
213
214         if (!S_ISREG(st->st_mode))
215                 GOTO(out_release, rc = 0);
216
217         fd = lli->lli_file_data;
218
219         lsm = lli->lli_smd;
220         if (lsm == NULL) {
221                 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
222                         CDEBUG(D_INODE, "object creation was delayed\n");
223                         GOTO(out_release, rc);
224                 }
225         }
226         fd->fd_flags &= ~O_LOV_DELAY_CREATE;
227
228         lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
229
230  out_release:
231         request = it->d.lustre.it_data;
232         ptlrpc_req_finished(request);
233
234         it->it_op_release(it);
235         OBD_FREE(it, sizeof(*it));
236
237         /* libsysio hasn't done anything for O_TRUNC. here we
238          * simply simulate it as open(...); truncate(...); */
239         if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
240                 struct iattr attr;
241
242                 memset(&attr, 0, sizeof(attr));
243                 attr.ia_size = 0;
244                 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
245                 rc = llu_setattr_raw(inode, &attr);
246                 if (rc)
247                         CERROR("error %d truncate in open()\n", rc);
248         }
249
250         liblustre_wait_event(0);
251         RETURN(rc);
252 }
253
254 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
255 {
256         struct mds_body *body;
257         struct lov_mds_md *eadata;
258         struct lov_stripe_md *lsm = NULL;
259         struct obd_trans_info oti = { 0 };
260         struct obdo *oa;
261         int rc;
262         ENTRY;
263
264         /* req is swabbed so this is safe */
265         body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
266
267         if (!(body->valid & OBD_MD_FLEASIZE))
268                 RETURN(0);
269
270         if (body->eadatasize == 0) {
271                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
272                 GOTO(out, rc = -EPROTO);
273         }
274
275         /* The MDS sent back the EA because we unlinked the last reference
276          * to this file. Use this EA to unlink the objects on the OST.
277          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
278          * check it is complete and sensible. */
279         eadata = lustre_swab_repbuf(request, REPLY_REC_OFF+1, body->eadatasize,
280                                     NULL);
281         LASSERT(eadata != NULL);
282         if (eadata == NULL) {
283                 CERROR("Can't unpack MDS EA data\n");
284                 GOTO(out, rc = -EPROTO);
285         }
286
287         rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
288         if (rc < 0) {
289                 CERROR("obd_unpackmd: %d\n", rc);
290                 GOTO(out, rc);
291         }
292         LASSERT(rc >= sizeof(*lsm));
293
294         OBDO_ALLOC(oa);
295         if (oa == NULL)
296                 GOTO(out_free_memmd, rc = -ENOMEM);
297
298         oa->o_id = lsm->lsm_object_id;
299         oa->o_mode = body->mode & S_IFMT;
300         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
301
302         if (body->valid & OBD_MD_FLCOOKIE) {
303                 oa->o_valid |= OBD_MD_FLCOOKIE;
304                 oti.oti_logcookies =
305                         lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
306                                        sizeof(struct llog_cookie) *
307                                        lsm->lsm_stripe_count);
308                 if (oti.oti_logcookies == NULL) {
309                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
310                         body->valid &= ~OBD_MD_FLCOOKIE;
311                 }
312         }
313
314         rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL);
315         OBDO_FREE(oa);
316         if (rc)
317                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
318                        lsm->lsm_object_id, rc);
319  out_free_memmd:
320         obd_free_memmd(llu_i2obdexp(dir), &lsm);
321  out:
322         return rc;
323 }
324
325 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
326 {
327         struct llu_inode_info *lli = llu_i2info(inode);
328         struct intnl_stat *st = llu_i2stat(inode);
329         struct ll_file_data *fd = lli->lli_file_data;
330         struct ptlrpc_request *req = NULL;
331         struct obd_client_handle *och = &fd->fd_mds_och;
332         struct obdo obdo = { 0 };
333         struct mdc_op_data data = { { 0 } };
334         int rc, valid;
335         ENTRY;
336
337         /* clear group lock, if present */
338         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
339                 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
340                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
341                 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
342                                        &fd->fd_cwlockh);
343         }
344
345         obdo.o_id = st->st_ino;
346         obdo.o_valid = OBD_MD_FLID;
347         valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
348                 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
349         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
350                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
351
352         obdo_from_inode(&obdo, inode, valid);
353
354         if (0 /* ll_is_inode_dirty(inode) */) {
355                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
356                 obdo.o_valid |= OBD_MD_FLFLAGS;
357         }
358         data.fid1 = lli->lli_fid;
359         rc = mdc_close(mdc_exp, &data, &obdo, och, &req);
360         if (rc == EAGAIN) {
361                 /* We are the last writer, so the MDS has instructed us to get
362                  * the file size and any write cookies, then close again. */
363                 //ll_queue_done_writing(inode);
364                 rc = 0;
365         } else if (rc) {
366                 CERROR("inode %llu close failed: rc %d\n",
367                        (long long)st->st_ino, rc);
368         } else {
369                 rc = llu_objects_destroy(req, inode);
370                 if (rc)
371                         CERROR("inode %llu ll_objects destroy: rc = %d\n",
372                                (long long)st->st_ino, rc);
373         }
374
375         mdc_clear_open_replay_data(och);
376         ptlrpc_req_finished(req);
377         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
378         lli->lli_file_data = NULL;
379         OBD_FREE(fd, sizeof(*fd));
380
381         RETURN(rc);
382 }
383
384 int llu_file_release(struct inode *inode)
385 {
386         struct ll_file_data *fd;
387         struct llu_sb_info *sbi = llu_i2sbi(inode);
388         struct llu_inode_info *lli = llu_i2info(inode);
389         int rc = 0, rc2;
390
391         ENTRY;
392         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
393                (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
394
395         if (llu_is_root_inode(inode))
396                 RETURN(0);
397
398         /* still opened by others? */
399         if (--lli->lli_open_count)
400                 RETURN(0);
401
402         fd = lli->lli_file_data;
403         if (!fd) /* no process opened the file after an mcreate */
404                 RETURN(0);
405
406         rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
407         if (rc2 && !rc)
408                 rc = rc2;
409
410         RETURN(rc);
411 }
412
413 /*
414  * libsysio require us return 0
415  */
416 int llu_iop_close(struct inode *inode)
417 {
418         int rc;
419         struct ldlm_res_id res_id =
420                 { .name = {llu_i2stat(inode)->st_ino,
421                  (__u64)llu_i2info(inode)->lli_st_generation, LDLM_FLOCK} };
422         struct lustre_handle lockh = {0};
423
424         liblustre_wait_event(0);
425
426         /* If we have posix locks on this file - clear all of those */
427         if (ldlm_lock_match(
428                       class_exp2obd(llu_i2mdcexp(inode))->obd_namespace,
429                       LDLM_FL_BLOCK_GRANTED|LDLM_FL_TEST_LOCK|LDLM_FL_CBPENDING,
430                       &res_id, LDLM_FLOCK, NULL, LCK_PR|LCK_PW, &lockh)) {
431                 struct file_lock lock;
432                 lock.fl_type = F_UNLCK;
433                 lock.fl_flags = FL_POSIX;
434                 lock.fl_start = 0;
435                 lock.fl_end = OFFSET_MAX;
436                 lock.fl_pid = getpid();
437                 lock.fl_notify = NULL;
438                 lock.fl_insert = NULL;
439                 lock.fl_remove = NULL;
440                 lock.fl_owner = NULL;
441                 lock.fl_file = NULL;
442
443                 llu_file_flock(inode, F_SETLK, &lock);
444         }
445
446         rc = llu_file_release(inode);
447         if (rc) {
448                 CERROR("file close error %d\n", rc);
449         }
450         /* if open count == 0 && stale_flag is set, should we
451          * remove the inode immediately? */
452         liblustre_wait_idle();
453         return 0;
454 }
455
456 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
457 {
458         ENTRY;
459
460         liblustre_wait_event(0);
461
462         if (off < 0 || off > ll_file_maxbytes(ino))
463                 RETURN(-EINVAL);
464
465         RETURN(off);
466 }
467
468 /* this isn't where truncate starts.  roughly:
469  * llu_iop_{open,setattr}->llu_setattr_raw->llu_vmtruncate->llu_truncate
470  * we grab the lock back in setattr_raw to avoid races. */
471 static void llu_truncate(struct inode *inode, obd_flag flags)
472 {
473         struct llu_inode_info *lli = llu_i2info(inode);
474         struct intnl_stat *st = llu_i2stat(inode);
475         struct obd_info oinfo = { { { 0 } } };
476         struct obdo oa = { 0 };
477         obd_valid valid;
478         int rc;
479         ENTRY;
480         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p) to %llu\n",
481                (long long)st->st_ino, lli->lli_st_generation, inode,
482                (long long)st->st_size);
483
484         if (!lli->lli_smd) {
485                 CDEBUG(D_INODE, "truncate on inode %llu with no objects\n",
486                        (long long)st->st_ino);
487                 EXIT;
488                 return;
489         }
490
491         oinfo.oi_md = lli->lli_smd;
492         oinfo.oi_policy.l_extent.start = st->st_size;
493         oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
494         oinfo.oi_oa = &oa;
495         oa.o_id = lli->lli_smd->lsm_object_id;
496         oa.o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
497         oa.o_flags = flags; /* We don't actually want to copy inode flags */
498  
499         valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLATIME;
500         if (flags & OBD_FL_TRUNCLOCK) {
501                 /* lockless truncate
502                  *
503                  * 1. do not use inode's timestamps because concurrent
504                  * stat might fill the inode with out-of-date times,
505                  * send current instead
506                  *
507                  * 2.do no update lsm, as long as stat (via
508                  * llu_glimpse_size) will bring attributes from osts
509                  * anyway */
510                 oa.o_mtime = oa.o_ctime = CURRENT_TIME;
511                 oa.o_valid |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
512         } else {
513                 /* truncate under locks
514                  *
515                  * 1. update inode's mtime and ctime as long as
516                  * concurrent stat (via llu_glimpse_size) might bring
517                  * out-of-date ones
518                  *
519                  * 2. update lsm so that next stat (via
520                  * llu_glimpse_size) could get correct values in lsm */
521                 struct ost_lvb xtimes;
522
523                 lov_stripe_lock(lli->lli_smd);
524                 st->st_mtime = st->st_ctime = CURRENT_TIME;
525                 xtimes.lvb_mtime = st->st_mtime;
526                 xtimes.lvb_ctime = st->st_ctime;
527                 obd_update_lvb(llu_i2obdexp(inode), lli->lli_smd, &xtimes,
528                                OBD_MD_FLMTIME | OBD_MD_FLCTIME);
529                 lov_stripe_unlock(lli->lli_smd);
530
531                 valid |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
532         }
533         obdo_from_inode(&oa, inode, valid);
534
535         obd_adjust_kms(llu_i2obdexp(inode), lli->lli_smd, st->st_size, 1);
536
537         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
538                oa.o_id, (long long)st->st_size);
539
540         /* truncate == punch from new size to absolute end of file */
541         rc = obd_punch_rqset(llu_i2obdexp(inode), &oinfo, NULL);
542         if (rc)
543                 CERROR("obd_truncate fails (%d) ino %llu\n",
544                        rc, (long long)st->st_ino);
545         else
546                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
547                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
548                                           OBD_MD_FLCTIME);
549
550         EXIT;
551         return;
552 } /* llu_truncate */
553
554 int llu_vmtruncate(struct inode * inode, loff_t offset, obd_flag flags)
555 {
556         llu_i2stat(inode)->st_size = offset;
557
558         /*
559          * llu_truncate() is only called from this
560          * point. llu_vmtruncate/llu_truncate split exists to mimic the
561          * structure of Linux VFS truncate code path.
562          */
563
564         llu_truncate(inode, flags);
565
566         return 0;
567 }