Whamcloud - gitweb
Fix valid uninitialized variable gcc warning.
[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 Super operations
5  *
6  *  Copyright (c) 2002, 2003 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/queue.h>
32
33 #include <sysio.h>
34 #include <fs.h>
35 #include <mount.h>
36 #include <inode.h>
37 #include <file.h>
38
39 #include "llite_lib.h"
40
41 void llu_prepare_mdc_op_data(struct mdc_op_data *data,
42                              struct inode *i1,
43                              struct inode *i2,
44                              const char *name,
45                              int namelen,
46                              int mode)
47 {
48         LASSERT(i1);
49         
50         ll_i2uctxt(&data->ctxt, i1, i2);
51         ll_inode2fid(&data->fid1, i1);
52
53         if (i2) {
54                 ll_inode2fid(&data->fid2, i2);
55         }
56
57         data->name = name;
58         data->namelen = namelen;
59         data->create_mode = mode;
60         data->mod_time = CURRENT_TIME;
61 }
62
63 void obdo_refresh_inode(struct inode *dst,
64                         struct obdo *src,
65                         obd_flag valid)
66 {
67         struct llu_inode_info *lli = llu_i2info(dst);
68         valid &= src->o_valid;
69
70         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
71                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
72                        src->o_valid, LTIME_S(lli->lli_st_mtime), 
73                        LTIME_S(lli->lli_st_ctime),
74                        (long)src->o_mtime, (long)src->o_ctime);
75
76         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
77                 LTIME_S(lli->lli_st_atime) = src->o_atime;
78         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
79                 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
80         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
81                 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
82         if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
83                 lli->lli_st_size = src->o_size;
84         /* optimum IO size */
85         if (valid & OBD_MD_FLBLKSZ)
86                 lli->lli_st_blksize = src->o_blksize;
87         /* allocation of space */
88         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
89                 lli->lli_st_blocks = src->o_blocks;
90 }
91
92 #if 0
93 static int llu_create_obj(struct lustre_handle *conn, struct inode *inode,
94                           struct lov_stripe_md *lsm)
95 {
96         struct ptlrpc_request *req = NULL;
97         struct llu_inode_info *lli = llu_i2info(inode);
98         struct lov_mds_md *lmm = NULL;
99         struct obdo *oa;
100         struct iattr iattr;
101         struct mdc_op_data op_data;
102         struct obd_trans_info oti = { 0 };
103         int rc, err, lmm_size = 0;;
104         ENTRY;
105
106         oa = obdo_alloc();
107         if (!oa)
108                 RETURN(-ENOMEM);
109
110         LASSERT(S_ISREG(inode->i_mode));
111         oa->o_mode = S_IFREG | 0600;
112         oa->o_id = lli->lli_st_ino;
113         oa->o_generation = lli->lli_st_generation;
114         /* Keep these 0 for now, because chown/chgrp does not change the
115          * ownership on the OST, and we don't want to allow BA OST NFS
116          * users to access these objects by mistake.
117          */
118         oa->o_uid = 0;
119         oa->o_gid = 0;
120         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
121                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID;
122
123         obdo_from_inode(oa, inode, OBD_MD_FLTYPE|OBD_MD_FLATIME|OBD_MD_FLMTIME|
124                         OBD_MD_FLCTIME |
125                         (llu_i2info(inode)->lli_st_size ? OBD_MD_FLSIZE : 0));
126
127         rc = obd_create(conn, oa, &lsm, &oti);
128         if (rc) {
129                 CERROR("error creating objects for inode %lu: rc = %d\n",
130                        lli->lli_st_ino, rc);
131                 if (rc > 0) {
132                         CERROR("obd_create returned invalid rc %d\n", rc);
133                         rc = -EIO;
134                 }
135                 GOTO(out_oa, rc);
136         }
137         obdo_refresh_inode(inode, oa, OBD_MD_FLBLKSZ);
138
139         LASSERT(lsm && lsm->lsm_object_id);
140         rc = obd_packmd(conn, &lmm, lsm);
141         if (rc < 0)
142                 GOTO(out_destroy, rc);
143
144         lmm_size = rc;
145
146         /* Save the stripe MD with this file on the MDS */
147         memset(&iattr, 0, sizeof(iattr));
148         iattr.ia_valid = ATTR_FROM_OPEN;
149
150         llu_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
151
152         rc = mdc_setattr(&llu_i2sbi(inode)->ll_mdc_conn, &op_data,
153                          &iattr, lmm, lmm_size, oti.oti_logcookies,
154                          oti.oti_numcookies * sizeof(oti.oti_onecookie), &req);
155         ptlrpc_req_finished(req);
156
157         obd_free_diskmd(conn, &lmm);
158
159         /* If we couldn't complete mdc_open() and store the stripe MD on the
160          * MDS, we need to destroy the objects now or they will be leaked.
161          */
162         if (rc) {
163                 CERROR("error: storing stripe MD for %lu: rc %d\n",
164                        lli->lli_st_ino, rc);
165                 GOTO(out_destroy, rc);
166         }
167         lli->lli_smd = lsm;
168         lli->lli_maxbytes = lsm->lsm_maxbytes;
169
170         EXIT;
171 out_oa:
172         oti_free_cookies(&oti);
173         obdo_free(oa);
174         return rc;
175
176 out_destroy:
177         oa->o_id = lsm->lsm_object_id;
178         oa->o_valid = OBD_MD_FLID;
179         obdo_from_inode(oa, inode, OBD_MD_FLTYPE);
180
181         err = obd_destroy(conn, oa, lsm, NULL);
182         obd_free_memmd(conn, &lsm);
183         if (err) {
184                 CERROR("error uncreating inode %lu objects: rc %d\n",
185                        lli->lli_st_ino, err);
186         }
187         goto out_oa;
188 }
189 #endif
190
191 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
192 {
193         struct ptlrpc_request *req = it->d.lustre.it_data;
194         struct ll_file_data *fd;
195         struct mds_body *body;
196         ENTRY;
197
198         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
199         LASSERT (body != NULL);                 /* reply already checked out */
200         LASSERT_REPSWABBED (req, 1);            /* and swabbed down */
201
202         /* already opened? */
203         if (lli->lli_open_count++)
204                 RETURN(0);
205                 
206         LASSERT(!lli->lli_file_data);
207
208         OBD_ALLOC(fd, sizeof(*fd));
209         /* We can't handle this well without reorganizing ll_file_open and
210          * ll_mdc_close, so don't even try right now. */
211         LASSERT(fd != NULL);
212
213         memset(fd, 0, sizeof(*fd));
214
215         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
216         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
217         lli->lli_file_data = fd;
218
219         mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
220
221         RETURN(0);
222 }
223
224 #if 0
225 static int llu_osc_open(struct lustre_handle *conn, struct inode *inode,
226                         struct lov_stripe_md *lsm)
227 {
228         struct ll_file_data *fd = llu_i2info(inode)->lli_file_data;
229         struct obdo *oa;
230         int rc;
231         ENTRY;
232
233         oa = obdo_alloc();
234         if (!oa)
235                 RETURN(-ENOMEM);
236         oa->o_id = lsm->lsm_object_id;
237         oa->o_mode = S_IFREG;
238         oa->o_valid = (OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLBLOCKS |
239                        OBD_MD_FLMTIME | OBD_MD_FLCTIME);
240         rc = obd_open(conn, oa, lsm, NULL, &fd->fd_ost_och);
241         if (rc)
242                 GOTO(out, rc);
243
244         /* file->f_flags &= ~O_LOV_DELAY_CREATE; */
245         obdo_to_inode(inode, oa, OBD_MD_FLBLOCKS | OBD_MD_FLMTIME |
246                       OBD_MD_FLCTIME);
247
248         EXIT;
249 out:
250         obdo_free(oa);
251         return rc;
252 }
253 #endif
254
255
256 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
257 {
258         struct inode *inode = pnode->p_base->pb_ino;
259         struct llu_inode_info *lli = llu_i2info(inode);
260         struct ll_file_data *fd;
261         struct ptlrpc_request *request;
262         struct lookup_intent *it;
263         struct lov_stripe_md *lsm;
264         int rc = 0;
265         ENTRY;
266
267         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
268         LL_GET_INTENT(inode, it);
269
270         if (!it->d.lustre.it_disposition) {
271 #if 0
272                 struct lookup_intent oit = { .it_op = IT_OPEN,
273                                              .it_flags = file->f_flags };
274                 it = &oit;
275                 rc = ll_intent_file_open(file, NULL, 0, it);
276                 if (rc)
277                         GOTO(out_release, rc);
278 #endif
279                 CERROR("fixme!!\n");
280         }
281
282         rc = it_open_error(DISP_OPEN_OPEN, it);
283         if (rc)
284                 GOTO(out_release, rc);
285
286         rc = llu_local_open(lli, it);
287         if (rc)
288                 LBUG();
289
290         if (!S_ISREG(lli->lli_st_mode))
291                 GOTO(out_release, rc = 0);
292                 
293         fd = lli->lli_file_data;
294
295         lsm = lli->lli_smd;
296         if (lsm == NULL) {
297                 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
298                         CDEBUG(D_INODE, "object creation was delayed\n");
299                         GOTO(out_release, rc);
300                 }
301 #if 0
302                 if (!lli->lli_smd) {
303                         rc = llu_create_obj(conn, inode, NULL);
304                         if (rc)
305                                 GOTO(out_close, rc);
306                 } else {
307                         CERROR("warning: stripe already set on ino %lu\n",
308                                lli->lli_st_ino);
309                 }
310                 lsm = lli->lli_smd;
311 #endif
312         }
313         fd->fd_flags &= ~O_LOV_DELAY_CREATE;
314
315  out_release:
316         request = it->d.lustre.it_data;
317         ptlrpc_req_finished(request);
318
319         it->it_op_release(it);
320         OBD_FREE(it, sizeof(*it));
321
322         RETURN(rc);
323 }
324
325 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
326 {
327         struct mds_body *body;
328         struct lov_mds_md *eadata;
329         struct lov_stripe_md *lsm = NULL;
330         struct obd_trans_info oti = { 0 };
331         struct obdo *oa;
332         int rc;
333         ENTRY;
334
335         /* req is swabbed so this is safe */
336         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
337
338         if (!(body->valid & OBD_MD_FLEASIZE))
339                 RETURN(0);
340
341         if (body->eadatasize == 0) {
342                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
343                 GOTO(out, rc = -EPROTO);
344         }
345
346         /* The MDS sent back the EA because we unlinked the last reference
347          * to this file. Use this EA to unlink the objects on the OST.
348          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
349          * check it is complete and sensible. */
350         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
351         LASSERT(eadata != NULL);
352         if (eadata == NULL) {
353                 CERROR("Can't unpack MDS EA data\n");
354                 GOTO(out, rc = -EPROTO);
355         }
356
357         rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
358         if (rc < 0) {
359                 CERROR("obd_unpackmd: %d\n", rc);
360                 GOTO(out, rc);
361         }
362         LASSERT(rc >= sizeof(*lsm));
363
364         oa = obdo_alloc();
365         if (oa == NULL)
366                 GOTO(out_free_memmd, rc = -ENOMEM);
367
368         oa->o_id = lsm->lsm_object_id;
369         oa->o_mode = body->mode & S_IFMT;
370         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
371
372         if (body->valid & OBD_MD_FLCOOKIE) {
373                 oa->o_valid |= OBD_MD_FLCOOKIE;
374                 oti.oti_logcookies =
375                         lustre_msg_buf(request->rq_repmsg, 2,
376                                        sizeof(struct llog_cookie) *
377                                        lsm->lsm_stripe_count);
378                 if (oti.oti_logcookies == NULL) {
379                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
380                         body->valid &= ~OBD_MD_FLCOOKIE;
381                 }
382         }
383
384         rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
385         obdo_free(oa);
386         if (rc)
387                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
388                        lsm->lsm_object_id, rc);
389  out_free_memmd:
390         obd_free_memmd(llu_i2obdexp(dir), &lsm);
391  out:
392         return rc;
393 }
394
395 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
396 {
397         struct llu_inode_info *lli = llu_i2info(inode);
398         struct ll_file_data *fd = lli->lli_file_data;
399         struct ptlrpc_request *req = NULL;
400         struct obd_client_handle *och = &fd->fd_mds_och;
401         struct obdo obdo;
402         int rc, valid;
403         ENTRY;
404
405         valid = OBD_MD_FLID;
406         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
407                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
408
409         memset(&obdo, 0, sizeof(obdo));
410         obdo.o_id = lli->lli_st_ino;
411         obdo.o_mode = lli->lli_st_mode;
412         obdo.o_size = lli->lli_st_size;
413         obdo.o_blocks = lli->lli_st_blocks;
414         if (0 /* ll_is_inode_dirty(inode) */) {
415                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
416                 valid |= OBD_MD_FLFLAGS;
417         }
418         obdo.o_valid = valid;
419         rc = mdc_close(mdc_exp, &obdo, och, &req);
420         if (rc == EAGAIN) {
421                 /* We are the last writer, so the MDS has instructed us to get
422                  * the file size and any write cookies, then close again. */
423                 //ll_queue_done_writing(inode);
424                 rc = 0;
425         } else if (rc) {
426                 CERROR("inode %lu close failed: rc = %d\n", lli->lli_st_ino, rc);
427         } else {
428                 rc = llu_objects_destroy(req, inode);
429                 if (rc)
430                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
431                                 lli->lli_st_ino, rc);
432         }
433
434         mdc_clear_open_replay_data(och);
435         ptlrpc_req_finished(req);
436         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
437         lli->lli_file_data = NULL;
438         OBD_FREE(fd, sizeof(*fd));
439
440         RETURN(rc);
441 }
442
443 int llu_file_release(struct inode *inode)
444 {
445         struct ll_file_data *fd;
446         struct llu_sb_info *sbi = llu_i2sbi(inode);
447         struct llu_inode_info *lli = llu_i2info(inode);
448         int rc = 0, rc2;
449
450         ENTRY;
451         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
452                lli->lli_st_generation);
453
454         /* FIXME need add this check later. how to find the root pnode? */
455 #if 0
456         /* don't do anything for / */
457         if (inode->i_sb->s_root == file->f_dentry)
458                 RETURN(0);
459 #endif
460         /* still opened by others? */
461         if (--lli->lli_open_count)
462                 RETURN(0);
463
464         fd = lli->lli_file_data;
465         if (!fd) /* no process opened the file after an mcreate */
466                 RETURN(0);
467
468         rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
469         if (rc2 && !rc)
470                 rc = rc2;
471
472         RETURN(rc);
473 }
474
475 int llu_iop_close(struct inode *inode)
476 {
477         int rc;
478
479         rc = llu_file_release(inode);
480         if (!llu_i2info(inode)->lli_open_count)
481                 llu_i2info(inode)->lli_stale_flag = 1;
482         return rc;
483 }
484
485 int llu_iop_ipreadv(struct inode *ino,
486                     struct ioctx *ioctx)
487 {
488         ENTRY;
489
490         if (!ioctx->ioctx_iovlen)
491                 RETURN(0);
492         if (ioctx->ioctx_iovlen < 0)
493                 RETURN(-EINVAL);
494
495         ioctx->ioctx_private = llu_file_read(ino,
496                                         ioctx->ioctx_iovec,
497                                         ioctx->ioctx_iovlen,
498                                         ioctx->ioctx_offset);
499         if (IS_ERR(ioctx->ioctx_private))
500                 return (PTR_ERR(ioctx->ioctx_private));
501
502         RETURN(0);
503 }
504
505 int llu_iop_ipwritev(struct inode *ino,
506                      struct ioctx *ioctx)
507 {
508         ENTRY;
509
510         if (!ioctx->ioctx_iovlen)
511                 RETURN(0);
512         if (ioctx->ioctx_iovlen < 0)
513                 RETURN(-EINVAL);
514
515         ioctx->ioctx_private = llu_file_write(ino,
516                                          ioctx->ioctx_iovec,
517                                          ioctx->ioctx_iovlen,
518                                          ioctx->ioctx_offset);
519         if (IS_ERR(ioctx->ioctx_private))
520                 return (PTR_ERR(ioctx->ioctx_private));
521
522         RETURN(0);
523 }
524
525 /* this isn't where truncate starts.   roughly:
526  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
527  * we grab the lock back in setattr_raw to avoid races. */
528 static void llu_truncate(struct inode *inode)
529 {
530         struct llu_inode_info *lli = llu_i2info(inode);
531         struct lov_stripe_md *lsm = lli->lli_smd;
532         struct obdo oa = {0};
533         int err;
534         ENTRY;
535         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
536                lli->lli_st_generation);
537
538         if (!lsm) {
539                 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
540                 EXIT;
541                 return;
542         }
543
544         oa.o_id = lsm->lsm_object_id;
545         oa.o_valid = OBD_MD_FLID;
546         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
547                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
548
549         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
550                oa.o_id, lli->lli_st_size);
551
552         /* truncate == punch from new size to absolute end of file */
553         err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
554                         OBD_OBJECT_EOF, NULL);
555         if (err)
556                 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
557         else
558                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
559                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
560                                           OBD_MD_FLCTIME);
561
562         EXIT;
563         return;
564 }
565
566 int llu_vmtruncate(struct inode * inode, loff_t offset)
567 {
568         struct llu_inode_info *lli = llu_i2info(inode);
569
570         lli->lli_st_size = offset;
571
572         llu_truncate(inode);
573
574         return 0;
575 }