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