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