[POWERPC] PS3: Vuart add async read
[linux-2.6] / fs / quota.c
1 /*
2  * Quota code necessary even when VFS quota support is not compiled
3  * into the kernel.  The interesting stuff is over in dquot.c, here
4  * we have symbols for initial quotactl(2) handling, the sysctl(2)
5  * variables, etc - things needed even when quota support disabled.
6  */
7
8 #include <linux/fs.h>
9 #include <linux/namei.h>
10 #include <linux/slab.h>
11 #include <asm/current.h>
12 #include <asm/uaccess.h>
13 #include <linux/kernel.h>
14 #include <linux/smp_lock.h>
15 #include <linux/security.h>
16 #include <linux/syscalls.h>
17 #include <linux/buffer_head.h>
18 #include <linux/capability.h>
19 #include <linux/quotaops.h>
20
21 /* Check validity of generic quotactl commands */
22 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
23 {
24         if (type >= MAXQUOTAS)
25                 return -EINVAL;
26         if (!sb && cmd != Q_SYNC)
27                 return -ENODEV;
28         /* Is operation supported? */
29         if (sb && !sb->s_qcop)
30                 return -ENOSYS;
31
32         switch (cmd) {
33                 case Q_GETFMT:
34                         break;
35                 case Q_QUOTAON:
36                         if (!sb->s_qcop->quota_on)
37                                 return -ENOSYS;
38                         break;
39                 case Q_QUOTAOFF:
40                         if (!sb->s_qcop->quota_off)
41                                 return -ENOSYS;
42                         break;
43                 case Q_SETINFO:
44                         if (!sb->s_qcop->set_info)
45                                 return -ENOSYS;
46                         break;
47                 case Q_GETINFO:
48                         if (!sb->s_qcop->get_info)
49                                 return -ENOSYS;
50                         break;
51                 case Q_SETQUOTA:
52                         if (!sb->s_qcop->set_dqblk)
53                                 return -ENOSYS;
54                         break;
55                 case Q_GETQUOTA:
56                         if (!sb->s_qcop->get_dqblk)
57                                 return -ENOSYS;
58                         break;
59                 case Q_SYNC:
60                         if (sb && !sb->s_qcop->quota_sync)
61                                 return -ENOSYS;
62                         break;
63                 default:
64                         return -EINVAL;
65         }
66
67         /* Is quota turned on for commands which need it? */
68         switch (cmd) {
69                 case Q_GETFMT:
70                 case Q_GETINFO:
71                 case Q_QUOTAOFF:
72                 case Q_SETINFO:
73                 case Q_SETQUOTA:
74                 case Q_GETQUOTA:
75                         /* This is just informative test so we are satisfied without a lock */
76                         if (!sb_has_quota_enabled(sb, type))
77                                 return -ESRCH;
78         }
79
80         /* Check privileges */
81         if (cmd == Q_GETQUOTA) {
82                 if (((type == USRQUOTA && current->euid != id) ||
83                      (type == GRPQUOTA && !in_egroup_p(id))) &&
84                     !capable(CAP_SYS_ADMIN))
85                         return -EPERM;
86         }
87         else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
88                 if (!capable(CAP_SYS_ADMIN))
89                         return -EPERM;
90
91         return 0;
92 }
93
94 /* Check validity of XFS Quota Manager commands */
95 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
96 {
97         if (type >= XQM_MAXQUOTAS)
98                 return -EINVAL;
99         if (!sb)
100                 return -ENODEV;
101         if (!sb->s_qcop)
102                 return -ENOSYS;
103
104         switch (cmd) {
105                 case Q_XQUOTAON:
106                 case Q_XQUOTAOFF:
107                 case Q_XQUOTARM:
108                         if (!sb->s_qcop->set_xstate)
109                                 return -ENOSYS;
110                         break;
111                 case Q_XGETQSTAT:
112                         if (!sb->s_qcop->get_xstate)
113                                 return -ENOSYS;
114                         break;
115                 case Q_XSETQLIM:
116                         if (!sb->s_qcop->set_xquota)
117                                 return -ENOSYS;
118                         break;
119                 case Q_XGETQUOTA:
120                         if (!sb->s_qcop->get_xquota)
121                                 return -ENOSYS;
122                         break;
123                 case Q_XQUOTASYNC:
124                         if (!sb->s_qcop->quota_sync)
125                                 return -ENOSYS;
126                         break;
127                 default:
128                         return -EINVAL;
129         }
130
131         /* Check privileges */
132         if (cmd == Q_XGETQUOTA) {
133                 if (((type == XQM_USRQUOTA && current->euid != id) ||
134                      (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
135                      !capable(CAP_SYS_ADMIN))
136                         return -EPERM;
137         } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
138                 if (!capable(CAP_SYS_ADMIN))
139                         return -EPERM;
140         }
141
142         return 0;
143 }
144
145 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
146 {
147         int error;
148
149         if (XQM_COMMAND(cmd))
150                 error = xqm_quotactl_valid(sb, type, cmd, id);
151         else
152                 error = generic_quotactl_valid(sb, type, cmd, id);
153         if (!error)
154                 error = security_quotactl(cmd, type, id, sb);
155         return error;
156 }
157
158 static void quota_sync_sb(struct super_block *sb, int type)
159 {
160         int cnt;
161         struct inode *discard[MAXQUOTAS];
162
163         sb->s_qcop->quota_sync(sb, type);
164         /* This is not very clever (and fast) but currently I don't know about
165          * any other simple way of getting quota data to disk and we must get
166          * them there for userspace to be visible... */
167         if (sb->s_op->sync_fs)
168                 sb->s_op->sync_fs(sb, 1);
169         sync_blockdev(sb->s_bdev);
170
171         /* Now when everything is written we can discard the pagecache so
172          * that userspace sees the changes. We need i_mutex and so we could
173          * not do it inside dqonoff_mutex. Moreover we need to be carefull
174          * about races with quotaoff() (that is the reason why we have own
175          * reference to inode). */
176         mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
177         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
178                 discard[cnt] = NULL;
179                 if (type != -1 && cnt != type)
180                         continue;
181                 if (!sb_has_quota_enabled(sb, cnt))
182                         continue;
183                 discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
184         }
185         mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
186         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
187                 if (discard[cnt]) {
188                         mutex_lock(&discard[cnt]->i_mutex);
189                         truncate_inode_pages(&discard[cnt]->i_data, 0);
190                         mutex_unlock(&discard[cnt]->i_mutex);
191                         iput(discard[cnt]);
192                 }
193         }
194 }
195
196 void sync_dquots(struct super_block *sb, int type)
197 {
198         int cnt, dirty;
199
200         if (sb) {
201                 if (sb->s_qcop->quota_sync)
202                         quota_sync_sb(sb, type);
203                 return;
204         }
205
206         spin_lock(&sb_lock);
207 restart:
208         list_for_each_entry(sb, &super_blocks, s_list) {
209                 /* This test just improves performance so it needn't be reliable... */
210                 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
211                         if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
212                             && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
213                                 dirty = 1;
214                 if (!dirty)
215                         continue;
216                 sb->s_count++;
217                 spin_unlock(&sb_lock);
218                 down_read(&sb->s_umount);
219                 if (sb->s_root && sb->s_qcop->quota_sync)
220                         quota_sync_sb(sb, type);
221                 up_read(&sb->s_umount);
222                 spin_lock(&sb_lock);
223                 if (__put_super_and_need_restart(sb))
224                         goto restart;
225         }
226         spin_unlock(&sb_lock);
227 }
228
229 /* Copy parameters and call proper function */
230 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
231 {
232         int ret;
233
234         switch (cmd) {
235                 case Q_QUOTAON: {
236                         char *pathname;
237
238                         if (IS_ERR(pathname = getname(addr)))
239                                 return PTR_ERR(pathname);
240                         ret = sb->s_qcop->quota_on(sb, type, id, pathname);
241                         putname(pathname);
242                         return ret;
243                 }
244                 case Q_QUOTAOFF:
245                         return sb->s_qcop->quota_off(sb, type);
246
247                 case Q_GETFMT: {
248                         __u32 fmt;
249
250                         down_read(&sb_dqopt(sb)->dqptr_sem);
251                         if (!sb_has_quota_enabled(sb, type)) {
252                                 up_read(&sb_dqopt(sb)->dqptr_sem);
253                                 return -ESRCH;
254                         }
255                         fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
256                         up_read(&sb_dqopt(sb)->dqptr_sem);
257                         if (copy_to_user(addr, &fmt, sizeof(fmt)))
258                                 return -EFAULT;
259                         return 0;
260                 }
261                 case Q_GETINFO: {
262                         struct if_dqinfo info;
263
264                         if ((ret = sb->s_qcop->get_info(sb, type, &info)))
265                                 return ret;
266                         if (copy_to_user(addr, &info, sizeof(info)))
267                                 return -EFAULT;
268                         return 0;
269                 }
270                 case Q_SETINFO: {
271                         struct if_dqinfo info;
272
273                         if (copy_from_user(&info, addr, sizeof(info)))
274                                 return -EFAULT;
275                         return sb->s_qcop->set_info(sb, type, &info);
276                 }
277                 case Q_GETQUOTA: {
278                         struct if_dqblk idq;
279
280                         if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
281                                 return ret;
282                         if (copy_to_user(addr, &idq, sizeof(idq)))
283                                 return -EFAULT;
284                         return 0;
285                 }
286                 case Q_SETQUOTA: {
287                         struct if_dqblk idq;
288
289                         if (copy_from_user(&idq, addr, sizeof(idq)))
290                                 return -EFAULT;
291                         return sb->s_qcop->set_dqblk(sb, type, id, &idq);
292                 }
293                 case Q_SYNC:
294                         sync_dquots(sb, type);
295                         return 0;
296
297                 case Q_XQUOTAON:
298                 case Q_XQUOTAOFF:
299                 case Q_XQUOTARM: {
300                         __u32 flags;
301
302                         if (copy_from_user(&flags, addr, sizeof(flags)))
303                                 return -EFAULT;
304                         return sb->s_qcop->set_xstate(sb, flags, cmd);
305                 }
306                 case Q_XGETQSTAT: {
307                         struct fs_quota_stat fqs;
308                 
309                         if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
310                                 return ret;
311                         if (copy_to_user(addr, &fqs, sizeof(fqs)))
312                                 return -EFAULT;
313                         return 0;
314                 }
315                 case Q_XSETQLIM: {
316                         struct fs_disk_quota fdq;
317
318                         if (copy_from_user(&fdq, addr, sizeof(fdq)))
319                                 return -EFAULT;
320                        return sb->s_qcop->set_xquota(sb, type, id, &fdq);
321                 }
322                 case Q_XGETQUOTA: {
323                         struct fs_disk_quota fdq;
324
325                         if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
326                                 return ret;
327                         if (copy_to_user(addr, &fdq, sizeof(fdq)))
328                                 return -EFAULT;
329                         return 0;
330                 }
331                 case Q_XQUOTASYNC:
332                         return sb->s_qcop->quota_sync(sb, type);
333                 /* We never reach here unless validity check is broken */
334                 default:
335                         BUG();
336         }
337         return 0;
338 }
339
340 /*
341  * look up a superblock on which quota ops will be performed
342  * - use the name of a block device to find the superblock thereon
343  */
344 static inline struct super_block *quotactl_block(const char __user *special)
345 {
346 #ifdef CONFIG_BLOCK
347         struct block_device *bdev;
348         struct super_block *sb;
349         char *tmp = getname(special);
350
351         if (IS_ERR(tmp))
352                 return ERR_PTR(PTR_ERR(tmp));
353         bdev = lookup_bdev(tmp);
354         putname(tmp);
355         if (IS_ERR(bdev))
356                 return ERR_PTR(PTR_ERR(bdev));
357         sb = get_super(bdev);
358         bdput(bdev);
359         if (!sb)
360                 return ERR_PTR(-ENODEV);
361
362         return sb;
363 #else
364         return ERR_PTR(-ENODEV);
365 #endif
366 }
367
368 /*
369  * This is the system call interface. This communicates with
370  * the user-level programs. Currently this only supports diskquota
371  * calls. Maybe we need to add the process quotas etc. in the future,
372  * but we probably should use rlimits for that.
373  */
374 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
375 {
376         uint cmds, type;
377         struct super_block *sb = NULL;
378         int ret;
379
380         cmds = cmd >> SUBCMDSHIFT;
381         type = cmd & SUBCMDMASK;
382
383         if (cmds != Q_SYNC || special) {
384                 sb = quotactl_block(special);
385                 if (IS_ERR(sb))
386                         return PTR_ERR(sb);
387         }
388
389         ret = check_quotactl_valid(sb, type, cmds, id);
390         if (ret >= 0)
391                 ret = do_quotactl(sb, type, cmds, id, addr);
392         if (sb)
393                 drop_super(sb);
394
395         return ret;
396 }