]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/quota.c
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.
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/security.h>
15 #include <linux/syscalls.h>
16 #include <linux/buffer_head.h>
17 #include <linux/capability.h>
18 #include <linux/quotaops.h>
20 /* Check validity of generic quotactl commands */
21 static int generic_quotactl_valid(struct super_block
*sb
, int type
, int cmd
, qid_t id
)
23 if (type
>= MAXQUOTAS
)
25 if (!sb
&& cmd
!= Q_SYNC
)
27 /* Is operation supported? */
28 if (sb
&& !sb
->s_qcop
)
35 if (!sb
->s_qcop
->quota_on
)
39 if (!sb
->s_qcop
->quota_off
)
43 if (!sb
->s_qcop
->set_info
)
47 if (!sb
->s_qcop
->get_info
)
51 if (!sb
->s_qcop
->set_dqblk
)
55 if (!sb
->s_qcop
->get_dqblk
)
59 if (sb
&& !sb
->s_qcop
->quota_sync
)
66 /* Is quota turned on for commands which need it? */
74 /* This is just informative test so we are satisfied without a lock */
75 if (!sb_has_quota_enabled(sb
, type
))
79 /* Check privileges */
80 if (cmd
== Q_GETQUOTA
) {
81 if (((type
== USRQUOTA
&& current
->euid
!= id
) ||
82 (type
== GRPQUOTA
&& !in_egroup_p(id
))) &&
83 !capable(CAP_SYS_ADMIN
))
86 else if (cmd
!= Q_GETFMT
&& cmd
!= Q_SYNC
&& cmd
!= Q_GETINFO
)
87 if (!capable(CAP_SYS_ADMIN
))
93 /* Check validity of XFS Quota Manager commands */
94 static int xqm_quotactl_valid(struct super_block
*sb
, int type
, int cmd
, qid_t id
)
96 if (type
>= XQM_MAXQUOTAS
)
107 if (!sb
->s_qcop
->set_xstate
)
111 if (!sb
->s_qcop
->get_xstate
)
115 if (!sb
->s_qcop
->set_xquota
)
119 if (!sb
->s_qcop
->get_xquota
)
123 if (!sb
->s_qcop
->quota_sync
)
130 /* Check privileges */
131 if (cmd
== Q_XGETQUOTA
) {
132 if (((type
== XQM_USRQUOTA
&& current
->euid
!= id
) ||
133 (type
== XQM_GRPQUOTA
&& !in_egroup_p(id
))) &&
134 !capable(CAP_SYS_ADMIN
))
136 } else if (cmd
!= Q_XGETQSTAT
&& cmd
!= Q_XQUOTASYNC
) {
137 if (!capable(CAP_SYS_ADMIN
))
144 static int check_quotactl_valid(struct super_block
*sb
, int type
, int cmd
, qid_t id
)
148 if (XQM_COMMAND(cmd
))
149 error
= xqm_quotactl_valid(sb
, type
, cmd
, id
);
151 error
= generic_quotactl_valid(sb
, type
, cmd
, id
);
153 error
= security_quotactl(cmd
, type
, id
, sb
);
157 static void quota_sync_sb(struct super_block
*sb
, int type
)
160 struct inode
*discard
[MAXQUOTAS
];
162 sb
->s_qcop
->quota_sync(sb
, type
);
163 /* This is not very clever (and fast) but currently I don't know about
164 * any other simple way of getting quota data to disk and we must get
165 * them there for userspace to be visible... */
166 if (sb
->s_op
->sync_fs
)
167 sb
->s_op
->sync_fs(sb
, 1);
168 sync_blockdev(sb
->s_bdev
);
170 /* Now when everything is written we can discard the pagecache so
171 * that userspace sees the changes. We need i_mutex and so we could
172 * not do it inside dqonoff_mutex. Moreover we need to be carefull
173 * about races with quotaoff() (that is the reason why we have own
174 * reference to inode). */
175 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
176 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
178 if (type
!= -1 && cnt
!= type
)
180 if (!sb_has_quota_enabled(sb
, cnt
))
182 discard
[cnt
] = igrab(sb_dqopt(sb
)->files
[cnt
]);
184 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
185 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
187 mutex_lock(&discard
[cnt
]->i_mutex
);
188 truncate_inode_pages(&discard
[cnt
]->i_data
, 0);
189 mutex_unlock(&discard
[cnt
]->i_mutex
);
195 void sync_dquots(struct super_block
*sb
, int type
)
200 if (sb
->s_qcop
->quota_sync
)
201 quota_sync_sb(sb
, type
);
207 list_for_each_entry(sb
, &super_blocks
, s_list
) {
208 /* This test just improves performance so it needn't be reliable... */
209 for (cnt
= 0, dirty
= 0; cnt
< MAXQUOTAS
; cnt
++)
210 if ((type
== cnt
|| type
== -1) && sb_has_quota_enabled(sb
, cnt
)
211 && info_any_dirty(&sb_dqopt(sb
)->info
[cnt
]))
216 spin_unlock(&sb_lock
);
217 down_read(&sb
->s_umount
);
218 if (sb
->s_root
&& sb
->s_qcop
->quota_sync
)
219 quota_sync_sb(sb
, type
);
220 up_read(&sb
->s_umount
);
222 if (__put_super_and_need_restart(sb
))
225 spin_unlock(&sb_lock
);
228 /* Copy parameters and call proper function */
229 static int do_quotactl(struct super_block
*sb
, int type
, int cmd
, qid_t id
, void __user
*addr
)
237 if (IS_ERR(pathname
= getname(addr
)))
238 return PTR_ERR(pathname
);
239 ret
= sb
->s_qcop
->quota_on(sb
, type
, id
, pathname
);
244 return sb
->s_qcop
->quota_off(sb
, type
);
249 down_read(&sb_dqopt(sb
)->dqptr_sem
);
250 if (!sb_has_quota_enabled(sb
, type
)) {
251 up_read(&sb_dqopt(sb
)->dqptr_sem
);
254 fmt
= sb_dqopt(sb
)->info
[type
].dqi_format
->qf_fmt_id
;
255 up_read(&sb_dqopt(sb
)->dqptr_sem
);
256 if (copy_to_user(addr
, &fmt
, sizeof(fmt
)))
261 struct if_dqinfo info
;
263 if ((ret
= sb
->s_qcop
->get_info(sb
, type
, &info
)))
265 if (copy_to_user(addr
, &info
, sizeof(info
)))
270 struct if_dqinfo info
;
272 if (copy_from_user(&info
, addr
, sizeof(info
)))
274 return sb
->s_qcop
->set_info(sb
, type
, &info
);
279 if ((ret
= sb
->s_qcop
->get_dqblk(sb
, type
, id
, &idq
)))
281 if (copy_to_user(addr
, &idq
, sizeof(idq
)))
288 if (copy_from_user(&idq
, addr
, sizeof(idq
)))
290 return sb
->s_qcop
->set_dqblk(sb
, type
, id
, &idq
);
293 sync_dquots(sb
, type
);
301 if (copy_from_user(&flags
, addr
, sizeof(flags
)))
303 return sb
->s_qcop
->set_xstate(sb
, flags
, cmd
);
306 struct fs_quota_stat fqs
;
308 if ((ret
= sb
->s_qcop
->get_xstate(sb
, &fqs
)))
310 if (copy_to_user(addr
, &fqs
, sizeof(fqs
)))
315 struct fs_disk_quota fdq
;
317 if (copy_from_user(&fdq
, addr
, sizeof(fdq
)))
319 return sb
->s_qcop
->set_xquota(sb
, type
, id
, &fdq
);
322 struct fs_disk_quota fdq
;
324 if ((ret
= sb
->s_qcop
->get_xquota(sb
, type
, id
, &fdq
)))
326 if (copy_to_user(addr
, &fdq
, sizeof(fdq
)))
331 return sb
->s_qcop
->quota_sync(sb
, type
);
332 /* We never reach here unless validity check is broken */
340 * look up a superblock on which quota ops will be performed
341 * - use the name of a block device to find the superblock thereon
343 static inline struct super_block
*quotactl_block(const char __user
*special
)
346 struct block_device
*bdev
;
347 struct super_block
*sb
;
348 char *tmp
= getname(special
);
351 return ERR_PTR(PTR_ERR(tmp
));
352 bdev
= lookup_bdev(tmp
);
355 return ERR_PTR(PTR_ERR(bdev
));
356 sb
= get_super(bdev
);
359 return ERR_PTR(-ENODEV
);
363 return ERR_PTR(-ENODEV
);
368 * This is the system call interface. This communicates with
369 * the user-level programs. Currently this only supports diskquota
370 * calls. Maybe we need to add the process quotas etc. in the future,
371 * but we probably should use rlimits for that.
373 asmlinkage
long sys_quotactl(unsigned int cmd
, const char __user
*special
, qid_t id
, void __user
*addr
)
376 struct super_block
*sb
= NULL
;
379 cmds
= cmd
>> SUBCMDSHIFT
;
380 type
= cmd
& SUBCMDMASK
;
382 if (cmds
!= Q_SYNC
|| special
) {
383 sb
= quotactl_block(special
);
388 ret
= check_quotactl_valid(sb
, type
, cmds
, id
);
390 ret
= do_quotactl(sb
, type
, cmds
, id
, addr
);