Whamcloud - gitweb
b=18016 fix index type in ll_read_ahead_page
[fs/lustre-release.git] / lustre / llite / llite_close.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 (c) 2003, 2010, Oracle and/or its affiliates. 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/llite/llite_close.c
37  *
38  * Lustre Lite routines to issue a secondary close after writeback
39  */
40
41 #include <linux/module.h>
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44
45 //#include <lustre_mdc.h>
46 #include <lustre_lite.h>
47 #include "llite_internal.h"
48
49 /** records that a write is in flight */
50 void vvp_write_pending(struct ccc_object *club, struct ccc_page *page)
51 {
52         struct ll_inode_info *lli = ll_i2info(club->cob_inode);
53
54         ENTRY;
55         cfs_spin_lock(&lli->lli_lock);
56         lli->lli_flags |= LLIF_SOM_DIRTY;
57         if (page != NULL && cfs_list_empty(&page->cpg_pending_linkage))
58                 cfs_list_add(&page->cpg_pending_linkage,
59                              &club->cob_pending_list);
60         cfs_spin_unlock(&lli->lli_lock);
61         EXIT;
62 }
63
64 /** records that a write has completed */
65 void vvp_write_complete(struct ccc_object *club, struct ccc_page *page)
66 {
67         struct ll_inode_info *lli = ll_i2info(club->cob_inode);
68         int rc = 0;
69
70         ENTRY;
71         cfs_spin_lock(&lli->lli_lock);
72         if (page != NULL && !cfs_list_empty(&page->cpg_pending_linkage)) {
73                 cfs_list_del_init(&page->cpg_pending_linkage);
74                 rc = 1;
75         }
76         cfs_spin_unlock(&lli->lli_lock);
77         if (rc)
78                 ll_queue_done_writing(club->cob_inode, 0);
79         EXIT;
80 }
81
82 /** Queues DONE_WRITING if
83  * - done writing is allowed;
84  * - inode has no no dirty pages; */
85 void ll_queue_done_writing(struct inode *inode, unsigned long flags)
86 {
87         struct ll_inode_info *lli = ll_i2info(inode);
88         struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
89         ENTRY;
90
91         cfs_spin_lock(&lli->lli_lock);
92         lli->lli_flags |= flags;
93
94         if ((lli->lli_flags & LLIF_DONE_WRITING) &&
95             cfs_list_empty(&club->cob_pending_list)) {
96                 struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
97
98                 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
99                          CWARN("ino %lu/%u(flags %lu) som valid it just after "
100                                "recovery\n",
101                                inode->i_ino, inode->i_generation,
102                                lli->lli_flags);
103                 /* DONE_WRITING is allowed and inode has no dirty page. */
104                 cfs_spin_lock(&lcq->lcq_lock);
105
106                 LASSERT(cfs_list_empty(&lli->lli_close_list));
107                 CDEBUG(D_INODE, "adding inode %lu/%u to close list\n",
108                        inode->i_ino, inode->i_generation);
109                 cfs_list_add_tail(&lli->lli_close_list, &lcq->lcq_head);
110
111                 /* Avoid a concurrent insertion into the close thread queue:
112                  * an inode is already in the close thread, open(), write(),
113                  * close() happen, epoch is closed as the inode is marked as
114                  * LLIF_EPOCH_PENDING. When pages are written inode should not
115                  * be inserted into the queue again, clear this flag to avoid
116                  * it. */
117                 lli->lli_flags &= ~LLIF_DONE_WRITING;
118
119                 cfs_waitq_signal(&lcq->lcq_waitq);
120                 cfs_spin_unlock(&lcq->lcq_lock);
121         }
122         cfs_spin_unlock(&lli->lli_lock);
123         EXIT;
124 }
125
126 /** Pack SOM attributes info @opdata for CLOSE, DONE_WRITING rpc. */
127 void ll_done_writing_attr(struct inode *inode, struct md_op_data *op_data)
128 {
129         struct ll_inode_info *lli = ll_i2info(inode);
130         ENTRY;
131
132         op_data->op_flags |= MF_SOM_CHANGE;
133         /* Check if Size-on-MDS attributes are valid. */
134         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
135                 CERROR("ino %lu/%u(flags %lu) som valid it just after "
136                        "recovery\n", inode->i_ino, inode->i_generation,
137                        lli->lli_flags);
138
139         if (!cl_local_size(inode)) {
140                 /* Send Size-on-MDS Attributes if valid. */
141                 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
142                                 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
143         }
144         EXIT;
145 }
146
147 /** Closes ioepoch and packs Size-on-MDS attribute if needed into @op_data. */
148 void ll_ioepoch_close(struct inode *inode, struct md_op_data *op_data,
149                       struct obd_client_handle **och, unsigned long flags)
150 {
151         struct ll_inode_info *lli = ll_i2info(inode);
152         struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
153         ENTRY;
154
155         cfs_spin_lock(&lli->lli_lock);
156         if (!(cfs_list_empty(&club->cob_pending_list))) {
157                 if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
158                         LASSERT(*och != NULL);
159                         LASSERT(lli->lli_pending_och == NULL);
160                         /* Inode is dirty and there is no pending write done
161                          * request yet, DONE_WRITE is to be sent later. */
162                         lli->lli_flags |= LLIF_EPOCH_PENDING;
163                         lli->lli_pending_och = *och;
164                         cfs_spin_unlock(&lli->lli_lock);
165
166                         inode = igrab(inode);
167                         LASSERT(inode);
168                         GOTO(out, 0);
169                 }
170                 if (flags & LLIF_DONE_WRITING) {
171                         /* Some pages are still dirty, it is early to send
172                          * DONE_WRITE. Wait untill all pages will be flushed
173                          * and try DONE_WRITE again later. */
174                         LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
175                         lli->lli_flags |= LLIF_DONE_WRITING;
176                         cfs_spin_unlock(&lli->lli_lock);
177
178                         inode = igrab(inode);
179                         LASSERT(inode);
180                         GOTO(out, 0);
181                 }
182         }
183         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID"\n",
184                ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
185         op_data->op_flags |= MF_EPOCH_CLOSE;
186
187         if (flags & LLIF_DONE_WRITING) {
188                 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
189                 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
190                 *och = lli->lli_pending_och;
191                 lli->lli_pending_och = NULL;
192                 lli->lli_flags &= ~LLIF_EPOCH_PENDING;
193         } else {
194                 /* Pack Size-on-MDS inode attributes only if they has changed */
195                 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
196                         cfs_spin_unlock(&lli->lli_lock);
197                         GOTO(out, 0);
198                 }
199
200                 /* There is a pending DONE_WRITE -- close epoch with no
201                  * attribute change. */
202                 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
203                         cfs_spin_unlock(&lli->lli_lock);
204                         GOTO(out, 0);
205                 }
206         }
207
208         LASSERT(cfs_list_empty(&club->cob_pending_list));
209         lli->lli_flags &= ~LLIF_SOM_DIRTY;
210         cfs_spin_unlock(&lli->lli_lock);
211         ll_done_writing_attr(inode, op_data);
212
213         EXIT;
214 out:
215         return;
216 }
217
218 /**
219  * Cliens updates SOM attributes on MDS (including llog cookies):
220  * obd_getattr with no lock and md_setattr.
221  */
222 int ll_som_update(struct inode *inode, struct md_op_data *op_data)
223 {
224         struct ll_inode_info *lli = ll_i2info(inode);
225         struct ptlrpc_request *request = NULL;
226         __u32 old_flags;
227         struct obdo *oa;
228         int rc;
229         ENTRY;
230
231         LASSERT(op_data != NULL);
232         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
233                 CERROR("ino %lu/%u(flags %lu) som valid it just after "
234                        "recovery\n", inode->i_ino, inode->i_generation,
235                        lli->lli_flags);
236
237         OBDO_ALLOC(oa);
238         if (!oa) {
239                 CERROR("can't allocate memory for Size-on-MDS update.\n");
240                 RETURN(-ENOMEM);
241         }
242
243         old_flags = op_data->op_flags;
244         op_data->op_flags = MF_SOM_CHANGE;
245
246         /* If inode is already in another epoch, skip getattr from OSTs. */
247         if (lli->lli_ioepoch == op_data->op_ioepoch) {
248                 rc = ll_inode_getattr(inode, oa, op_data->op_ioepoch,
249                                       old_flags & MF_GETATTR_LOCK);
250                 if (rc) {
251                         oa->o_valid = 0;
252                         if (rc == -ENOENT)
253                                 CDEBUG(D_INODE, "objid "LPX64" is destroyed\n",
254                                        lli->lli_smd->lsm_object_id);
255                         else
256                                 CERROR("inode_getattr failed (%d): unable to "
257                                        "send a Size-on-MDS attribute update "
258                                        "for inode %lu/%u\n", rc, inode->i_ino,
259                                        inode->i_generation);
260                 } else {
261                         CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n",
262                                PFID(&lli->lli_fid));
263                 }
264                 /* Install attributes into op_data. */
265                 md_from_obdo(op_data, oa, oa->o_valid);
266         }
267
268         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data,
269                         NULL, 0, NULL, 0, &request, NULL);
270         ptlrpc_req_finished(request);
271
272         OBDO_FREE(oa);
273         RETURN(rc);
274 }
275
276 /**
277  * Closes the ioepoch and packs all the attributes into @op_data for
278  * DONE_WRITING rpc.
279  */
280 static void ll_prepare_done_writing(struct inode *inode,
281                                     struct md_op_data *op_data,
282                                     struct obd_client_handle **och)
283 {
284         ll_ioepoch_close(inode, op_data, och, LLIF_DONE_WRITING);
285         /* If there is no @och, we do not do D_W yet. */
286         if (*och == NULL)
287                 return;
288
289         ll_pack_inode2opdata(inode, op_data, &(*och)->och_fh);
290         ll_prep_md_op_data(op_data, inode, NULL, NULL,
291                            0, 0, LUSTRE_OPC_ANY, NULL);
292 }
293
294 /** Send a DONE_WRITING rpc. */
295 static void ll_done_writing(struct inode *inode)
296 {
297         struct obd_client_handle *och = NULL;
298         struct md_op_data *op_data;
299         int rc;
300         ENTRY;
301
302         LASSERT(exp_connect_som(ll_i2mdexp(inode)));
303
304         OBD_ALLOC_PTR(op_data);
305         if (op_data == NULL) {
306                 CERROR("can't allocate op_data\n");
307                 EXIT;
308                 return;
309         }
310
311         ll_prepare_done_writing(inode, op_data, &och);
312         /* If there is no @och, we do not do D_W yet. */
313         if (och == NULL)
314                 GOTO(out, 0);
315
316         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
317         if (rc == -EAGAIN) {
318                 /* MDS has instructed us to obtain Size-on-MDS attribute from
319                  * OSTs and send setattr to back to MDS. */
320                 rc = ll_som_update(inode, op_data);
321         } else if (rc) {
322                 CERROR("inode %lu mdc done_writing failed: rc = %d\n",
323                        inode->i_ino, rc);
324         }
325 out:
326         ll_finish_md_op_data(op_data);
327         if (och) {
328                 md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
329                 OBD_FREE_PTR(och);
330         }
331         EXIT;
332 }
333
334 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
335 {
336         struct ll_inode_info *lli = NULL;
337
338         cfs_spin_lock(&lcq->lcq_lock);
339
340         if (!cfs_list_empty(&lcq->lcq_head)) {
341                 lli = cfs_list_entry(lcq->lcq_head.next, struct ll_inode_info,
342                                      lli_close_list);
343                 cfs_list_del_init(&lli->lli_close_list);
344         } else if (cfs_atomic_read(&lcq->lcq_stop))
345                 lli = ERR_PTR(-EALREADY);
346
347         cfs_spin_unlock(&lcq->lcq_lock);
348         return lli;
349 }
350
351 static int ll_close_thread(void *arg)
352 {
353         struct ll_close_queue *lcq = arg;
354         ENTRY;
355
356         {
357                 char name[CFS_CURPROC_COMM_MAX];
358                 snprintf(name, sizeof(name) - 1, "ll_close");
359                 cfs_daemonize(name);
360         }
361
362         cfs_complete(&lcq->lcq_comp);
363
364         while (1) {
365                 struct l_wait_info lwi = { 0 };
366                 struct ll_inode_info *lli;
367                 struct inode *inode;
368
369                 l_wait_event_exclusive(lcq->lcq_waitq,
370                                        (lli = ll_close_next_lli(lcq)) != NULL,
371                                        &lwi);
372                 if (IS_ERR(lli))
373                         break;
374
375                 inode = ll_info2i(lli);
376                 CDEBUG(D_INFO, "done_writting for inode %lu/%u\n",
377                        inode->i_ino, inode->i_generation);
378                 ll_done_writing(inode);
379                 iput(inode);
380         }
381
382         CDEBUG(D_INFO, "ll_close exiting\n");
383         cfs_complete(&lcq->lcq_comp);
384         RETURN(0);
385 }
386
387 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
388 {
389         struct ll_close_queue *lcq;
390         pid_t pid;
391
392         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
393                 return -EINTR;
394
395         OBD_ALLOC(lcq, sizeof(*lcq));
396         if (lcq == NULL)
397                 return -ENOMEM;
398
399         cfs_spin_lock_init(&lcq->lcq_lock);
400         CFS_INIT_LIST_HEAD(&lcq->lcq_head);
401         cfs_waitq_init(&lcq->lcq_waitq);
402         cfs_init_completion(&lcq->lcq_comp);
403
404         pid = cfs_kernel_thread(ll_close_thread, lcq, 0);
405         if (pid < 0) {
406                 OBD_FREE(lcq, sizeof(*lcq));
407                 return pid;
408         }
409
410         cfs_wait_for_completion(&lcq->lcq_comp);
411         *lcq_ret = lcq;
412         return 0;
413 }
414
415 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
416 {
417         cfs_init_completion(&lcq->lcq_comp);
418         cfs_atomic_inc(&lcq->lcq_stop);
419         cfs_waitq_signal(&lcq->lcq_waitq);
420         cfs_wait_for_completion(&lcq->lcq_comp);
421         OBD_FREE(lcq, sizeof(*lcq));
422 }