Whamcloud - gitweb
Branch b1_6
[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  * Lustre Lite routines to issue a secondary close after writeback
5  *
6  *  Copyright (c) 2001-2003 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 #include <linux/module.h>
25
26 #define DEBUG_SUBSYSTEM S_LLITE
27
28 #include <lustre_lite.h>
29 #include "llite_internal.h"
30
31 #ifdef HAVE_CLOSE_THREAD
32 /* record that a write is in flight */
33 void llap_write_pending(struct inode *inode, struct ll_async_page *llap)
34 {
35         struct ll_inode_info *lli = ll_i2info(inode);
36         spin_lock(&lli->lli_lock);
37         list_add(&llap->llap_pending_write, &lli->lli_pending_write_llaps);
38         spin_unlock(&lli->lli_lock);
39 }
40
41 /* record that a write has completed */
42 void llap_write_complete(struct inode *inode, struct ll_async_page *llap)
43 {
44         struct ll_inode_info *lli = ll_i2info(inode);
45         spin_lock(&lli->lli_lock);
46         list_del_init(&llap->llap_pending_write);
47         spin_unlock(&lli->lli_lock);
48 }
49
50 void ll_open_complete(struct inode *inode)
51 {
52         struct ll_inode_info *lli = ll_i2info(inode);
53         spin_lock(&lli->lli_lock);
54         lli->lli_send_done_writing = 0;
55         spin_unlock(&lli->lli_lock);
56 }
57
58 /* if we close with writes in flight then we want the completion or cancelation
59  * of those writes to send a DONE_WRITING rpc to the MDS */
60 int ll_is_inode_dirty(struct inode *inode)
61 {
62         struct ll_inode_info *lli = ll_i2info(inode);
63         int rc = 0;
64         ENTRY;
65
66         spin_lock(&lli->lli_lock);
67         if (!list_empty(&lli->lli_pending_write_llaps))
68                 rc = 1;
69         spin_unlock(&lli->lli_lock);
70         RETURN(rc);
71 }
72
73 void ll_try_done_writing(struct inode *inode)
74 {
75         struct ll_inode_info *lli = ll_i2info(inode);
76         struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
77
78         spin_lock(&lli->lli_lock);
79
80         if (lli->lli_send_done_writing &&
81             list_empty(&lli->lli_pending_write_llaps)) {
82
83                 spin_lock(&lcq->lcq_lock);
84                 if (list_empty(&lli->lli_close_item)) {
85                         CDEBUG(D_INODE, "adding inode %lu/%u to close list\n",
86                                inode->i_ino, inode->i_generation);
87                         igrab(inode);
88                         list_add_tail(&lli->lli_close_item, &lcq->lcq_list);
89                         wake_up(&lcq->lcq_waitq);
90                 }
91                 spin_unlock(&lcq->lcq_lock);
92         }
93
94         spin_unlock(&lli->lli_lock);
95 }
96
97 /* The MDS needs us to get the real file attributes, then send a DONE_WRITING */
98 void ll_queue_done_writing(struct inode *inode)
99 {
100         struct ll_inode_info *lli = ll_i2info(inode);
101         ENTRY;
102
103         spin_lock(&lli->lli_lock);
104         lli->lli_send_done_writing = 1;
105         spin_unlock(&lli->lli_lock);
106
107         ll_try_done_writing(inode);
108         EXIT;
109 }
110
111 /* If we know the file size and have the cookies:
112  *  - send a DONE_WRITING rpc
113  *
114  * Otherwise:
115  *  - get a whole-file lock
116  *  - get the authoritative size and all cookies with GETATTRs
117  *  - send a DONE_WRITING rpc
118  */
119 static void ll_close_done_writing(struct inode *inode)
120 {
121         struct ll_inode_info *lli = ll_i2info(inode);
122         ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF } };
123         struct lustre_handle lockh = { 0 };
124         struct obdo obdo;
125         obd_flag valid;
126         int rc, ast_flags = 0;
127         ENTRY;
128
129         memset(&obdo, 0, sizeof(obdo));
130         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
131                 goto rpc;
132
133         rc = ll_extent_lock(NULL, inode, lli->lli_smd, LCK_PW, &policy, &lockh,
134                             ast_flags);
135         if (rc != 0) {
136                 CERROR("lock acquisition failed (%d): unable to send "
137                        "DONE_WRITING for inode %lu/%u\n", rc, inode->i_ino,
138                        inode->i_generation);
139                 GOTO(out, rc);
140         }
141
142         rc = ll_lsm_getattr(ll_i2obdexp(inode), lli->lli_smd, &obdo);
143         if (rc) {
144                 CERROR("inode_getattr failed (%d): unable to send DONE_WRITING "
145                        "for inode %lu/%u\n", rc, inode->i_ino,
146                        inode->i_generation);
147                 ll_extent_unlock(NULL, inode, lli->lli_smd, LCK_PW, &lockh);
148                 GOTO(out, rc);
149         }
150
151         obdo_refresh_inode(inode, &obdo, valid);
152
153         CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
154                lli->lli_smd->lsm_object_id, i_size_read(inode), inode->i_blocks,
155                1<<inode->i_blkbits);
156
157         set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
158
159         rc = ll_extent_unlock(NULL, inode, lli->lli_smd, LCK_PW, &lockh);
160         if (rc != ELDLM_OK)
161                 CERROR("unlock failed (%d)?  proceeding anyways...\n", rc);
162
163  rpc:
164         obdo.o_id = inode->i_ino;
165         obdo.o_size = i_size_read(inode);
166         obdo.o_blocks = inode->i_blocks;
167         obdo.o_valid = OBD_MD_FLID | OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
168
169         rc = mdc_done_writing(ll_i2sbi(inode)->ll_mdc_exp, &obdo);
170  out:
171 }
172
173
174 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
175 {
176         struct ll_inode_info *lli = NULL;
177
178         spin_lock(&lcq->lcq_lock);
179
180         if (lcq->lcq_list.next == NULL)
181                 lli = ERR_PTR(-1);
182         else if (!list_empty(&lcq->lcq_list)) {
183                 lli = list_entry(lcq->lcq_list.next, struct ll_inode_info,
184                                  lli_close_item);
185                 list_del(&lli->lli_close_item);
186         }
187
188         spin_unlock(&lcq->lcq_lock);
189         return lli;
190 }
191 #else
192 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
193 {
194         if (lcq->lcq_list.next == NULL)
195                 return ERR_PTR(-1);
196
197         return NULL;
198 }
199 #endif
200
201 static int ll_close_thread(void *arg)
202 {
203         struct ll_close_queue *lcq = arg;
204         ENTRY;
205
206         {
207                 char name[CFS_CURPROC_COMM_MAX];
208                 snprintf(name, sizeof(name) - 1, "ll_close");
209                 cfs_daemonize(name);
210         }
211         
212         complete(&lcq->lcq_comp);
213
214         while (1) {
215                 struct l_wait_info lwi = { 0 };
216                 struct ll_inode_info *lli;
217                 //struct inode *inode;
218
219                 l_wait_event_exclusive(lcq->lcq_waitq,
220                                        (lli = ll_close_next_lli(lcq)) != NULL,
221                                        &lwi);
222                 if (IS_ERR(lli))
223                         break;
224
225                 //inode = ll_info2i(lli);
226                 //ll_close_done_writing(inode);
227                 //iput(inode);
228         }
229
230         complete(&lcq->lcq_comp);
231         RETURN(0);
232 }
233
234 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
235 {
236         struct ll_close_queue *lcq;
237         pid_t pid;
238
239         OBD_FAIL_RETURN(OBD_FAIL_LDLM_CLOSE_THREAD, -EINTR);
240         OBD_ALLOC(lcq, sizeof(*lcq));
241         if (lcq == NULL)
242                 return -ENOMEM;
243
244         spin_lock_init(&lcq->lcq_lock);
245         INIT_LIST_HEAD(&lcq->lcq_list);
246         init_waitqueue_head(&lcq->lcq_waitq);
247         init_completion(&lcq->lcq_comp);
248
249         pid = kernel_thread(ll_close_thread, lcq, 0);
250         if (pid < 0) {
251                 OBD_FREE(lcq, sizeof(*lcq));
252                 return pid;
253         }
254
255         wait_for_completion(&lcq->lcq_comp);
256         *lcq_ret = lcq;
257         return 0;
258 }
259
260 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
261 {
262         init_completion(&lcq->lcq_comp);
263         lcq->lcq_list.next = NULL;
264         wake_up(&lcq->lcq_waitq);
265         wait_for_completion(&lcq->lcq_comp);
266         OBD_FREE(lcq, sizeof(*lcq));
267 }
268
269