Whamcloud - gitweb
- fixes in split about using correct byte order;
[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_mdc.h>
29 #include <lustre_lite.h>
30 #include "llite_internal.h"
31
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         
37         ENTRY;
38         spin_lock(&lli->lli_lock);
39         lli->lli_flags |= LLIF_SOM_DIRTY;
40         if (llap && list_empty(&llap->llap_pending_write))
41                 list_add(&llap->llap_pending_write, 
42                          &lli->lli_pending_write_llaps);
43         spin_unlock(&lli->lli_lock);
44         EXIT;
45 }
46
47 /* record that a write has completed */
48 int llap_write_complete(struct inode *inode, struct ll_async_page *llap)
49 {
50         struct ll_inode_info *lli = ll_i2info(inode);
51         int rc = 0;
52         
53         ENTRY;
54         spin_lock(&lli->lli_lock);
55         if (llap && !list_empty(&llap->llap_pending_write)) {
56                 list_del_init(&llap->llap_pending_write);
57                 rc = 1;
58         }
59         spin_unlock(&lli->lli_lock);
60         RETURN(rc);
61 }
62
63 /* Queue DONE_WRITING if 
64  * - done writing is allowed;
65  * - inode has no no dirty pages; */
66 void ll_queue_done_writing(struct inode *inode, unsigned long flags)
67 {
68         struct ll_inode_info *lli = ll_i2info(inode);
69         
70         spin_lock(&lli->lli_lock);
71         lli->lli_flags |= flags;
72         
73         if ((lli->lli_flags & LLIF_DONE_WRITING) &&
74             list_empty(&lli->lli_pending_write_llaps)) {
75                 struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
76
77                 /* DONE_WRITING is allowed and inode has no dirty page. */
78                 spin_lock(&lcq->lcq_lock);
79
80                 /* 
81                  * XXX: Seems sometimes it is possible to try to add inode more
82                  * than once to close thread queue. Not sure if that is correct
83                  * from caller POV, but it looks to me logically to check this
84                  * here and just do nothing if inode is already on the queue.
85                  * Hope Vitaly will correct me if I'm wrong. --umka
86                  */
87                 if (list_empty(&lli->lli_close_list)) {
88                         CDEBUG(D_INODE, "adding inode %lu/%u to close list\n",
89                                inode->i_ino, inode->i_generation);
90                         list_add_tail(&lli->lli_close_list, &lcq->lcq_head);
91                 } else {
92                         CWARN("Inode %lu/%u is already queued for done writing!\n",
93                               inode->i_ino, inode->i_generation);
94                 }
95                 wake_up(&lcq->lcq_waitq);
96                 spin_unlock(&lcq->lcq_lock);
97         }
98         spin_unlock(&lli->lli_lock);
99 }
100
101 /* Close epoch and send Size-on-MDS attribute update if possible. 
102  * Call this under @lli->lli_lock spinlock. */
103 void ll_epoch_close(struct inode *inode, struct md_op_data *op_data,
104                     struct obd_client_handle **och, unsigned long flags)
105 {
106         struct ll_inode_info *lli = ll_i2info(inode);
107         ENTRY;
108
109         spin_lock(&lli->lli_lock);
110         if (!(list_empty(&lli->lli_pending_write_llaps)) && 
111             !(lli->lli_flags & LLIF_EPOCH_PENDING)) {
112                 LASSERT(*och != NULL);
113                 LASSERT(lli->lli_pending_och == NULL);
114                 /* Inode is dirty and there is no pending write done request
115                  * yet, DONE_WRITE is to be sent later. */
116                 lli->lli_flags |= LLIF_EPOCH_PENDING;
117                 lli->lli_pending_och = *och;
118                 spin_unlock(&lli->lli_lock);
119                 
120                 inode = igrab(inode);
121                 LASSERT(inode);
122                 GOTO(out, 0);
123         }
124
125         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID"\n",
126                op_data->ioepoch, PFID(&lli->lli_fid));
127         op_data->flags |= MF_EPOCH_CLOSE;
128
129         if (flags & LLIF_DONE_WRITING) {
130                 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
131                 *och = lli->lli_pending_och;
132                 lli->lli_pending_och = NULL;
133                 lli->lli_flags &= ~(LLIF_DONE_WRITING | LLIF_EPOCH_PENDING | 
134                                     LLIF_EPOCH_PENDING);
135         } else {
136                 /* Pack Size-on-MDS inode attributes only if they has changed */
137                 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
138                         spin_unlock(&lli->lli_lock);
139                         GOTO(out, 0);
140                 }
141
142                 /* There is already 1 pending DONE_WRITE, do not create another
143                  * one -- close epoch with no attribute change. */
144                 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
145                         spin_unlock(&lli->lli_lock);
146                         GOTO(out, 0);
147                 }
148         }
149         
150         spin_unlock(&lli->lli_lock);
151         op_data->flags |= MF_SOM_CHANGE;
152
153         /* Check if Size-on-MDS attributes are valid. */
154         LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
155         if (!ll_local_size(inode)) {
156                 /* Send Size-on-MDS Attributes if valid. */
157                 op_data->attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
158                                           ATTR_SIZE | ATTR_BLOCKS;
159         }
160         EXIT;
161 out:
162         return;
163 }
164
165 int ll_sizeonmds_update(struct inode *inode, struct lustre_handle *fh)
166 {
167         struct ll_inode_info *lli = ll_i2info(inode);
168         struct md_op_data *op_data;
169         struct obdo *oa;
170         int rc;
171         ENTRY;
172         
173         LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
174         
175         oa = obdo_alloc();
176         OBD_ALLOC_PTR(op_data);
177         if (!oa || !op_data) {
178                 CERROR("can't allocate memory for Size-on-MDS update.\n");
179                 RETURN(-ENOMEM);
180         }
181         rc = ll_inode_getattr(inode, oa);
182         if (rc) {
183                 CERROR("inode_getattr failed (%d): unable to send a "
184                        "Size-on-MDS attribute update for inode %lu/%u\n",
185                        rc, inode->i_ino, inode->i_generation);
186                 GOTO(out, rc);
187         }
188         CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n", PFID(&lli->lli_fid));
189         
190         md_from_obdo(op_data, oa, oa->o_valid);
191         memcpy(&op_data->handle, fh, sizeof(*fh));
192         
193         op_data->ioepoch = lli->lli_ioepoch;
194         op_data->flags |= MF_SOM_CHANGE;
195         
196         rc = ll_md_setattr(inode, op_data);
197         EXIT;
198 out:
199         if (oa)
200                 obdo_free(oa);
201         if (op_data)
202                 ll_finish_md_op_data(op_data);
203         return rc;
204 }
205
206 /* Send a DONE_WRITING rpc, pack Size-on-MDS attributes into it, if possible */
207 static void ll_done_writing(struct inode *inode)
208 {
209         struct obd_client_handle *och = NULL;
210         struct md_op_data *op_data;
211         int rc;
212         ENTRY;
213
214         OBD_ALLOC_PTR(op_data);
215         if (op_data == NULL) {
216                 CERROR("can't allocate op_data\n");
217                 EXIT;
218                 return;
219         }
220
221         ll_epoch_close(inode, op_data, &och, LLIF_DONE_WRITING);
222         ll_pack_inode2opdata(inode, op_data, &och->och_fh);
223
224         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, och);
225         ll_finish_md_op_data(op_data);
226         if (rc == -EAGAIN) {
227                 /* MDS has instructed us to obtain Size-on-MDS attribute from 
228                  * OSTs and send setattr to back to MDS. */
229                 rc = ll_sizeonmds_update(inode, &och->och_fh);
230         } else if (rc) {
231                 CERROR("inode %lu mdc done_writing failed: rc = %d\n",
232                        inode->i_ino, rc);
233         }
234         OBD_FREE_PTR(och);
235         EXIT;
236 }
237
238 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
239 {
240         struct ll_inode_info *lli = NULL;
241
242         spin_lock(&lcq->lcq_lock);
243
244         if (!list_empty(&lcq->lcq_head)) {
245                 lli = list_entry(lcq->lcq_head.next, struct ll_inode_info,
246                                  lli_close_list);
247                 list_del_init(&lli->lli_close_list);
248         } else if (atomic_read(&lcq->lcq_stop))
249                 lli = ERR_PTR(-EALREADY);
250
251         spin_unlock(&lcq->lcq_lock);
252         return lli;
253 }
254
255 static int ll_close_thread(void *arg)
256 {
257         struct ll_close_queue *lcq = arg;
258         ENTRY;
259
260         {
261                 char name[CFS_CURPROC_COMM_MAX];
262                 snprintf(name, sizeof(name) - 1, "ll_close");
263                 cfs_daemonize(name);
264         }
265         
266         complete(&lcq->lcq_comp);
267
268         while (1) {
269                 struct l_wait_info lwi = { 0 };
270                 struct ll_inode_info *lli;
271                 struct inode *inode;
272
273                 l_wait_event_exclusive(lcq->lcq_waitq,
274                                        (lli = ll_close_next_lli(lcq)) != NULL,
275                                        &lwi);
276                 if (IS_ERR(lli))
277                         break;
278
279                 inode = ll_info2i(lli);
280                 CDEBUG(D_INFO, "done_writting for inode %lu/%u\n",
281                        inode->i_ino, inode->i_generation);
282                 ll_done_writing(inode);
283                 iput(inode);
284         }
285
286         CDEBUG(D_INFO, "ll_close exiting\n");
287         complete(&lcq->lcq_comp);
288         RETURN(0);
289 }
290
291 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
292 {
293         struct ll_close_queue *lcq;
294         pid_t pid;
295
296         OBD_ALLOC(lcq, sizeof(*lcq));
297         if (lcq == NULL)
298                 return -ENOMEM;
299
300         spin_lock_init(&lcq->lcq_lock);
301         INIT_LIST_HEAD(&lcq->lcq_head);
302         init_waitqueue_head(&lcq->lcq_waitq);
303         init_completion(&lcq->lcq_comp);
304
305         pid = kernel_thread(ll_close_thread, lcq, 0);
306         if (pid < 0) {
307                 OBD_FREE(lcq, sizeof(*lcq));
308                 return pid;
309         }
310
311         wait_for_completion(&lcq->lcq_comp);
312         *lcq_ret = lcq;
313         return 0;
314 }
315
316 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
317 {
318         init_completion(&lcq->lcq_comp);
319         atomic_inc(&lcq->lcq_stop);
320         wake_up(&lcq->lcq_waitq);
321         wait_for_completion(&lcq->lcq_comp);
322         OBD_FREE(lcq, sizeof(*lcq));
323 }