Whamcloud - gitweb
- landing b_fid.
[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/queue.h>
32 #include <fcntl.h>
33
34 #include <xtio.h>
35 #include <sysio.h>
36 #include <fs.h>
37 #include <mount.h>
38 #include <inode.h>
39 #include <file.h>
40
41 #undef LIST_HEAD
42
43 #include "llite_lib.h"
44
45 void llu_prepare_mdc_data(struct mdc_op_data *data, struct inode *i1,
46                           struct inode *i2, const char *name,
47                           int namelen, int mode)
48 {
49         LASSERT(i1);
50         
51         ll_inode2id(&data->id1, i1);
52         if (i2)
53                 ll_inode2id(&data->id2, i2);
54
55         data->valid = 0;
56         data->name = name;
57         data->namelen = namelen;
58         data->create_mode = mode;
59         data->mod_time = CURRENT_TIME;
60 }
61
62 void obdo_refresh_inode(struct inode *dst,
63                         struct obdo *src,
64                         obd_flag valid)
65 {
66         struct llu_inode_info *lli = llu_i2info(dst);
67         valid &= src->o_valid;
68
69         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
70                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
71                        src->o_valid, LTIME_S(lli->lli_st_mtime), 
72                        LTIME_S(lli->lli_st_ctime),
73                        (long)src->o_mtime, (long)src->o_ctime);
74
75         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
76                 LTIME_S(lli->lli_st_atime) = src->o_atime;
77         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
78                 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
79         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
80                 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
81         if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
82                 lli->lli_st_size = src->o_size;
83         /* optimum IO size */
84         if (valid & OBD_MD_FLBLKSZ)
85                 lli->lli_st_blksize = src->o_blksize;
86         /* allocation of space */
87         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
88                 lli->lli_st_blocks = src->o_blocks;
89 }
90
91 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
92 {
93         struct ptlrpc_request *req = it->d.lustre.it_data;
94         struct ll_file_data *fd;
95         struct mds_body *body;
96         ENTRY;
97
98         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
99         LASSERT (body != NULL);                 /* reply already checked out */
100         LASSERT_REPSWABBED (req, 1);            /* and swabbed down */
101
102         /* already opened? */
103         if (lli->lli_open_count++)
104                 RETURN(0);
105
106         LASSERT(!lli->lli_file_data);
107
108         OBD_ALLOC(fd, sizeof(*fd));
109         /* We can't handle this well without reorganizing ll_file_open and
110          * ll_mdc_close, so don't even try right now. */
111         LASSERT(fd != NULL);
112
113         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
114         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
115         lli->lli_file_data = fd;
116
117         mdc_set_open_replay_data(NULL, &fd->fd_mds_och, it->d.lustre.it_data);
118
119         RETURN(0);
120 }
121
122 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
123 {
124         struct inode *inode = pnode->p_base->pb_ino;
125         struct llu_inode_info *lli = llu_i2info(inode);
126         struct ll_file_data *fd;
127         struct ptlrpc_request *request;
128         struct lookup_intent *it;
129         struct lov_stripe_md *lsm;
130         int rc = 0;
131         ENTRY;
132
133         liblustre_wait_event(0);
134
135         /* don't do anything for '/' */
136         if (llu_is_root_inode(inode))
137                 RETURN(0);
138
139         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
140         LL_GET_INTENT(inode, it);
141
142         if (!it->d.lustre.it_disposition) {
143                 LBUG();
144         }
145
146         rc = it_open_error(DISP_OPEN_OPEN, it);
147         if (rc)
148                 GOTO(out_release, rc);
149
150         rc = llu_local_open(lli, it);
151         if (rc)
152                 LBUG();
153
154         if (!S_ISREG(lli->lli_st_mode))
155                 GOTO(out_release, rc = 0);
156                 
157         fd = lli->lli_file_data;
158
159         lsm = lli->lli_smd;
160         if (lsm == NULL) {
161                 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
162                         CDEBUG(D_INODE, "object creation was delayed\n");
163                         GOTO(out_release, rc);
164                 }
165         }
166         fd->fd_flags &= ~O_LOV_DELAY_CREATE;
167
168         lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
169
170  out_release:
171         request = it->d.lustre.it_data;
172         ptlrpc_req_finished(request);
173
174         it->it_op_release(it);
175         OBD_FREE(it, sizeof(*it));
176
177         /* libsysio haven't doing anything for O_TRUNC. here we
178          * simply simulate it as open(...); truncate(...);
179          */
180         if (rc == 0 && (flags & O_TRUNC) &&
181             S_ISREG(lli->lli_st_mode)) {
182                 struct iattr attr;
183
184                 memset(&attr, 0, sizeof(attr));
185                 attr.ia_size = 0;
186                 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
187                 rc  = llu_setattr_raw(inode, &attr);
188                 if (rc) {
189                         CERROR("error %d truncate in open()\n", rc);
190                 }
191         }
192
193         RETURN(rc);
194 }
195
196 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
197 {
198         struct mds_body *body;
199         struct lov_mds_md *eadata;
200         struct lov_stripe_md *lsm = NULL;
201         struct obd_trans_info oti = { 0 };
202         struct obdo *oa;
203         int rc;
204         ENTRY;
205
206         /* req is swabbed so this is safe */
207         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
208
209         if (!(body->valid & OBD_MD_FLEASIZE))
210                 RETURN(0);
211
212         if (body->eadatasize == 0) {
213                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
214                 GOTO(out, rc = -EPROTO);
215         }
216
217         /* The MDS sent back the EA because we unlinked the last reference
218          * to this file. Use this EA to unlink the objects on the OST.
219          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
220          * check it is complete and sensible. */
221         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
222         LASSERT(eadata != NULL);
223         if (eadata == NULL) {
224                 CERROR("Can't unpack MDS EA data\n");
225                 GOTO(out, rc = -EPROTO);
226         }
227
228         rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
229         if (rc < 0) {
230                 CERROR("obd_unpackmd: %d\n", rc);
231                 GOTO(out, rc);
232         }
233         LASSERT(rc >= sizeof(*lsm));
234
235         oa = obdo_alloc();
236         if (oa == NULL)
237                 GOTO(out_free_memmd, rc = -ENOMEM);
238
239         oa->o_id = lsm->lsm_object_id;
240         oa->o_mode = body->mode & S_IFMT;
241         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
242
243         if (body->valid & OBD_MD_FLCOOKIE) {
244                 oa->o_valid |= OBD_MD_FLCOOKIE;
245                 oti.oti_logcookies =
246                         lustre_msg_buf(request->rq_repmsg, 2,
247                                        sizeof(struct llog_cookie) *
248                                        lsm->lsm_stripe_count);
249                 if (oti.oti_logcookies == NULL) {
250                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
251                         body->valid &= ~OBD_MD_FLCOOKIE;
252                 }
253         }
254
255         rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
256         obdo_free(oa);
257         if (rc)
258                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
259                        lsm->lsm_object_id, rc);
260  out_free_memmd:
261         obd_free_memmd(llu_i2obdexp(dir), &lsm);
262  out:
263         return rc;
264 }
265
266 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
267 {
268         struct llu_inode_info *lli = llu_i2info(inode);
269         struct ll_file_data *fd = lli->lli_file_data;
270         struct ptlrpc_request *req = NULL;
271         struct obd_client_handle *och = &fd->fd_mds_och;
272         struct obdo obdo;
273         int rc, valid;
274         ENTRY;
275
276         /* clear group lock, if present */
277         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
278                 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
279                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
280                 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
281                                        &fd->fd_cwlockh);
282         }
283
284         obdo.o_id = lli->lli_st_ino;
285         obdo.o_valid = OBD_MD_FLID;
286         valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
287                 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
288         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
289                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
290
291         obdo_from_inode(&obdo, inode, valid);
292
293         if (0 /* ll_is_inode_dirty(inode) */) {
294                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
295                 obdo.o_valid |= OBD_MD_FLFLAGS;
296         }
297         rc = mdc_close(mdc_exp, &obdo, och, &req);
298         if (rc == EAGAIN) {
299                 /* We are the last writer, so the MDS has instructed us to get
300                  * the file size and any write cookies, then close again. */
301                 //ll_queue_done_writing(inode);
302                 rc = 0;
303         } else if (rc) {
304                 CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
305         } else {
306                 rc = llu_objects_destroy(req, inode);
307                 if (rc)
308                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
309                                 lli->lli_st_ino, rc);
310         }
311
312         mdc_clear_open_replay_data(NULL, och);
313         ptlrpc_req_finished(req);
314         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
315         lli->lli_file_data = NULL;
316         OBD_FREE(fd, sizeof(*fd));
317
318         RETURN(rc);
319 }
320
321 int llu_file_release(struct inode *inode)
322 {
323         struct ll_file_data *fd;
324         struct llu_sb_info *sbi = llu_i2sbi(inode);
325         struct llu_inode_info *lli = llu_i2info(inode);
326         int rc = 0, rc2;
327
328         ENTRY;
329         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
330                lli->lli_st_generation);
331
332         if (llu_is_root_inode(inode))
333                 RETURN(0);
334
335         /* still opened by others? */
336         if (--lli->lli_open_count)
337                 RETURN(0);
338
339         fd = lli->lli_file_data;
340         if (!fd) /* no process opened the file after an mcreate */
341                 RETURN(0);
342
343         rc2 = llu_mdc_close(sbi->ll_lmv_exp, inode);
344         if (rc2 && !rc)
345                 rc = rc2;
346
347         RETURN(rc);
348 }
349
350 /*
351  * libsysio require us return 0
352  */
353 int llu_iop_close(struct inode *inode)
354 {
355         int rc;
356
357         liblustre_wait_event(0);
358
359         rc = llu_file_release(inode);
360         if (rc) {
361                 CERROR("file close error %d\n", rc);
362         }
363         /* if open count == 0 && stale_flag is set, should we
364          * remove the inode immediately? */
365         return 0;
366 }
367
368 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
369 {
370         ENTRY;
371
372         liblustre_wait_event(0);
373
374         if (off < 0 || off > ll_file_maxbytes(ino))
375                 RETURN(-EINVAL);
376
377         RETURN(off);
378 }
379
380 /* this isn't where truncate starts.   roughly:
381  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
382  * we grab the lock back in setattr_raw to avoid races. */
383 static void llu_truncate(struct inode *inode)
384 {
385         struct llu_inode_info *lli = llu_i2info(inode);
386         struct lov_stripe_md *lsm = lli->lli_smd;
387         struct obdo oa = {0};
388         int err;
389         ENTRY;
390         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
391                lli->lli_st_generation);
392
393         if (!lsm) {
394                 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
395                 EXIT;
396                 return;
397         }
398
399         oa.o_id = lsm->lsm_object_id;
400         oa.o_valid = OBD_MD_FLID;
401         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
402                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
403
404         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
405                oa.o_id, lli->lli_st_size);
406
407         /* truncate == punch from new size to absolute end of file */
408         err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
409                         OBD_OBJECT_EOF, NULL);
410         if (err)
411                 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
412         else
413                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
414                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
415                                           OBD_MD_FLCTIME);
416
417         EXIT;
418         return;
419 }
420
421 int llu_vmtruncate(struct inode * inode, loff_t offset)
422 {
423         struct llu_inode_info *lli = llu_i2info(inode);
424
425         lli->lli_st_size = offset;
426
427         llu_truncate(inode);
428
429         return 0;
430 }