Whamcloud - gitweb
b=16098
[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 [sun.com URL with a
20  * copy of GPLv2].
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/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 /* record that a write is in flight */
50 void llap_write_pending(struct inode *inode, struct ll_async_page *llap)
51 {
52         struct ll_inode_info *lli = ll_i2info(inode);
53
54         ENTRY;
55         spin_lock(&lli->lli_lock);
56         lli->lli_flags |= LLIF_SOM_DIRTY;
57         if (llap && list_empty(&llap->llap_pending_write))
58                 list_add(&llap->llap_pending_write, 
59                          &lli->lli_pending_write_llaps);
60         spin_unlock(&lli->lli_lock);
61         EXIT;
62 }
63
64 /* record that a write has completed */
65 int llap_write_complete(struct inode *inode, struct ll_async_page *llap)
66 {
67         struct ll_inode_info *lli = ll_i2info(inode);
68         int rc = 0;
69         
70         ENTRY;
71         spin_lock(&lli->lli_lock);
72         if (llap && !list_empty(&llap->llap_pending_write)) {
73                 list_del_init(&llap->llap_pending_write);
74                 rc = 1;
75         }
76         spin_unlock(&lli->lli_lock);
77         RETURN(rc);
78 }
79
80 /* Queue DONE_WRITING if 
81  * - done writing is allowed;
82  * - inode has no no dirty pages; */
83 void ll_queue_done_writing(struct inode *inode, unsigned long flags)
84 {
85         struct ll_inode_info *lli = ll_i2info(inode);
86
87         spin_lock(&lli->lli_lock);
88         lli->lli_flags |= flags;
89
90         if ((lli->lli_flags & LLIF_DONE_WRITING) &&
91             list_empty(&lli->lli_pending_write_llaps)) {
92                 struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
93
94                 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
95                          CWARN("ino %lu/%u(flags %lu) som valid it just after "
96                                "recovery\n",
97                                inode->i_ino, inode->i_generation, 
98                                lli->lli_flags);
99                 /* DONE_WRITING is allowed and inode has no dirty page. */
100                 spin_lock(&lcq->lcq_lock);
101
102                 LASSERT(list_empty(&lli->lli_close_list));
103                 CDEBUG(D_INODE, "adding inode %lu/%u to close list\n",
104                        inode->i_ino, inode->i_generation);
105                 list_add_tail(&lli->lli_close_list, &lcq->lcq_head);
106
107                 /* Avoid a concurrent insertion into the close thread queue:
108                  * an inode is already in the close thread, open(), write(),
109                  * close() happen, epoch is closed as the inode is marked as
110                  * LLIF_EPOCH_PENDING. When pages are written inode should not
111                  * be inserted into the queue again, clear this flag to avoid
112                  * it. */
113                 lli->lli_flags &= ~LLIF_DONE_WRITING;
114
115                 wake_up(&lcq->lcq_waitq);
116                 spin_unlock(&lcq->lcq_lock);
117         }
118         spin_unlock(&lli->lli_lock);
119 }
120
121 /* Close epoch and send Size-on-MDS attribute update if possible. 
122  * Call this under @lli->lli_lock spinlock. */
123 void ll_epoch_close(struct inode *inode, struct md_op_data *op_data,
124                     struct obd_client_handle **och, unsigned long flags)
125 {
126         struct ll_inode_info *lli = ll_i2info(inode);
127         ENTRY;
128
129         spin_lock(&lli->lli_lock);
130         if (!(list_empty(&lli->lli_pending_write_llaps))) {
131                 if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
132                         LASSERT(*och != NULL);
133                         LASSERT(lli->lli_pending_och == NULL);
134                         /* Inode is dirty and there is no pending write done
135                          * request yet, DONE_WRITE is to be sent later. */
136                         lli->lli_flags |= LLIF_EPOCH_PENDING;
137                         lli->lli_pending_och = *och;
138                         spin_unlock(&lli->lli_lock);
139
140                         inode = igrab(inode);
141                         LASSERT(inode);
142                         GOTO(out, 0);
143                 } 
144                 if (flags & LLIF_DONE_WRITING) {
145                         /* Some pages are still dirty, it is early to send
146                          * DONE_WRITE. Wait untill all pages will be flushed
147                          * and try DONE_WRITE again later. */
148                         LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
149                         lli->lli_flags |= LLIF_DONE_WRITING;
150                         spin_unlock(&lli->lli_lock);
151
152                         inode = igrab(inode);
153                         LASSERT(inode);
154                         GOTO(out, 0);
155                 }
156         }
157         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID"\n",
158                ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
159         op_data->op_flags |= MF_EPOCH_CLOSE;
160
161         if (flags & LLIF_DONE_WRITING) {
162                 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
163                 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
164                 *och = lli->lli_pending_och;
165                 lli->lli_pending_och = NULL;
166                 lli->lli_flags &= ~LLIF_EPOCH_PENDING;
167         } else {
168                 /* Pack Size-on-MDS inode attributes only if they has changed */
169                 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
170                         spin_unlock(&lli->lli_lock);
171                         GOTO(out, 0);
172                 }
173
174                 /* There is a pending DONE_WRITE -- close epoch with no
175                  * attribute change. */
176                 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
177                         spin_unlock(&lli->lli_lock);
178                         GOTO(out, 0);
179                 }
180         }
181
182         LASSERT(list_empty(&lli->lli_pending_write_llaps));
183         lli->lli_flags &= ~LLIF_SOM_DIRTY;
184         spin_unlock(&lli->lli_lock);
185         op_data->op_flags |= MF_SOM_CHANGE;
186
187         /* Check if Size-on-MDS attributes are valid. */
188         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
189                 CWARN("ino %lu/%u(flags %lu) som valid it just after "
190                       "recovery\n",
191                       inode->i_ino, inode->i_generation, lli->lli_flags);
192
193         if (!ll_local_size(inode)) {
194                 /* Send Size-on-MDS Attributes if valid. Atime is sent along
195                  * with all the attributes. */
196                 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
197                                 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
198         }
199         EXIT;
200 out:
201         return;
202 }
203
204 int ll_sizeonmds_update(struct inode *inode, struct md_open_data *mod,
205                         struct lustre_handle *fh, __u64 ioepoch)
206 {
207         struct ll_inode_info *lli = ll_i2info(inode);
208         struct md_op_data *op_data;
209         struct obdo *oa;
210         int rc;
211         ENTRY;
212         
213         /* LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)); */
214         /* After recovery that can be valid. */
215         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
216                 CWARN("ino %lu/%u(flags %lu) som valid it just after "
217                       "recovery\n", inode->i_ino, inode->i_generation,
218                       lli->lli_flags);
219
220         OBDO_ALLOC(oa);
221         OBD_ALLOC_PTR(op_data);
222         if (!oa || !op_data) {
223                 CERROR("can't allocate memory for Size-on-MDS update.\n");
224                 RETURN(-ENOMEM);
225         }
226         rc = ll_inode_getattr(inode, oa);
227         if (rc == -ENOENT) {
228                 oa->o_valid = 0;
229                 CDEBUG(D_INODE, "objid "LPX64" is already destroyed\n",
230                        lli->lli_smd->lsm_object_id);
231         } else if (rc) {
232                 CERROR("inode_getattr failed (%d): unable to send a "
233                        "Size-on-MDS attribute update for inode %lu/%u\n",
234                        rc, inode->i_ino, inode->i_generation);
235                 GOTO(out, rc);
236         }
237         CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n", PFID(&lli->lli_fid));
238
239         md_from_obdo(op_data, oa, oa->o_valid);
240         memcpy(&op_data->op_handle, fh, sizeof(*fh));
241
242         op_data->op_ioepoch = ioepoch;
243         op_data->op_flags |= MF_SOM_CHANGE;
244
245         rc = ll_md_setattr(inode, op_data, &mod);
246         EXIT;
247 out:
248         if (oa)
249                 OBDO_FREE(oa);
250         if (op_data)
251                 ll_finish_md_op_data(op_data);
252         return rc;
253 }
254
255 /* Send a DONE_WRITING rpc, pack Size-on-MDS attributes into it, if possible */
256 static void ll_done_writing(struct inode *inode)
257 {
258         struct obd_client_handle *och = NULL;
259         struct md_op_data *op_data;
260         int rc;
261         ENTRY;
262
263         LASSERT(ll_i2mdexp(inode)->exp_connect_flags & OBD_CONNECT_SOM);
264         
265         OBD_ALLOC_PTR(op_data);
266         if (op_data == NULL) {
267                 CERROR("can't allocate op_data\n");
268                 EXIT;
269                 return;
270         }
271
272         ll_epoch_close(inode, op_data, &och, LLIF_DONE_WRITING);
273         /* If there is no @och, we do not do D_W yet. */
274         if (och == NULL)
275                 GOTO(out, 0);
276         
277         ll_pack_inode2opdata(inode, op_data, &och->och_fh);
278
279         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, och->och_mod);
280         if (rc == -EAGAIN) {
281                 /* MDS has instructed us to obtain Size-on-MDS attribute from 
282                  * OSTs and send setattr to back to MDS. */
283                 rc = ll_sizeonmds_update(inode, och->och_mod,
284                                          &och->och_fh, op_data->op_ioepoch);
285         } else if (rc) {
286                 CERROR("inode %lu mdc done_writing failed: rc = %d\n",
287                        inode->i_ino, rc);
288         }
289 out:
290         ll_finish_md_op_data(op_data);
291         if (och) {
292                 md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
293                 OBD_FREE_PTR(och);
294         }
295         EXIT;
296 }
297
298 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
299 {
300         struct ll_inode_info *lli = NULL;
301
302         spin_lock(&lcq->lcq_lock);
303
304         if (!list_empty(&lcq->lcq_head)) {
305                 lli = list_entry(lcq->lcq_head.next, struct ll_inode_info,
306                                  lli_close_list);
307                 list_del_init(&lli->lli_close_list);
308         } else if (atomic_read(&lcq->lcq_stop))
309                 lli = ERR_PTR(-EALREADY);
310
311         spin_unlock(&lcq->lcq_lock);
312         return lli;
313 }
314
315 static int ll_close_thread(void *arg)
316 {
317         struct ll_close_queue *lcq = arg;
318         ENTRY;
319
320         {
321                 char name[CFS_CURPROC_COMM_MAX];
322                 snprintf(name, sizeof(name) - 1, "ll_close");
323                 cfs_daemonize(name);
324         }
325         
326         complete(&lcq->lcq_comp);
327
328         while (1) {
329                 struct l_wait_info lwi = { 0 };
330                 struct ll_inode_info *lli;
331                 struct inode *inode;
332
333                 l_wait_event_exclusive(lcq->lcq_waitq,
334                                        (lli = ll_close_next_lli(lcq)) != NULL,
335                                        &lwi);
336                 if (IS_ERR(lli))
337                         break;
338
339                 inode = ll_info2i(lli);
340                 CDEBUG(D_INFO, "done_writting for inode %lu/%u\n",
341                        inode->i_ino, inode->i_generation);
342                 ll_done_writing(inode);
343                 iput(inode);
344         }
345
346         CDEBUG(D_INFO, "ll_close exiting\n");
347         complete(&lcq->lcq_comp);
348         RETURN(0);
349 }
350
351 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
352 {
353         struct ll_close_queue *lcq;
354         pid_t pid;
355
356         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
357                 return -EINTR;
358
359         OBD_ALLOC(lcq, sizeof(*lcq));
360         if (lcq == NULL)
361                 return -ENOMEM;
362
363         spin_lock_init(&lcq->lcq_lock);
364         INIT_LIST_HEAD(&lcq->lcq_head);
365         init_waitqueue_head(&lcq->lcq_waitq);
366         init_completion(&lcq->lcq_comp);
367
368         pid = kernel_thread(ll_close_thread, lcq, 0);
369         if (pid < 0) {
370                 OBD_FREE(lcq, sizeof(*lcq));
371                 return pid;
372         }
373
374         wait_for_completion(&lcq->lcq_comp);
375         *lcq_ret = lcq;
376         return 0;
377 }
378
379 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
380 {
381         init_completion(&lcq->lcq_comp);
382         atomic_inc(&lcq->lcq_stop);
383         wake_up(&lcq->lcq_waitq);
384         wait_for_completion(&lcq->lcq_comp);
385         OBD_FREE(lcq, sizeof(*lcq));
386 }