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