quota: Fix possible corruption of dqi_flags
[sfrench/cifs-2.6.git] / fs / quota / kqid.c
1 #include <linux/fs.h>
2 #include <linux/quota.h>
3 #include <linux/export.h>
4
5 /**
6  *      qid_eq - Test to see if to kquid values are the same
7  *      @left: A qid value
8  *      @right: Another quid value
9  *
10  *      Return true if the two qid values are equal and false otherwise.
11  */
12 bool qid_eq(struct kqid left, struct kqid right)
13 {
14         if (left.type != right.type)
15                 return false;
16         switch(left.type) {
17         case USRQUOTA:
18                 return uid_eq(left.uid, right.uid);
19         case GRPQUOTA:
20                 return gid_eq(left.gid, right.gid);
21         case PRJQUOTA:
22                 return projid_eq(left.projid, right.projid);
23         default:
24                 BUG();
25         }
26 }
27 EXPORT_SYMBOL(qid_eq);
28
29 /**
30  *      qid_lt - Test to see if one qid value is less than another
31  *      @left: The possibly lesser qid value
32  *      @right: The possibly greater qid value
33  *
34  *      Return true if left is less than right and false otherwise.
35  */
36 bool qid_lt(struct kqid left, struct kqid right)
37 {
38         if (left.type < right.type)
39                 return true;
40         if (left.type > right.type)
41                 return false;
42         switch (left.type) {
43         case USRQUOTA:
44                 return uid_lt(left.uid, right.uid);
45         case GRPQUOTA:
46                 return gid_lt(left.gid, right.gid);
47         case PRJQUOTA:
48                 return projid_lt(left.projid, right.projid);
49         default:
50                 BUG();
51         }
52 }
53 EXPORT_SYMBOL(qid_lt);
54
55 /**
56  *      from_kqid - Create a qid from a kqid user-namespace pair.
57  *      @targ: The user namespace we want a qid in.
58  *      @kqid: The kernel internal quota identifier to start with.
59  *
60  *      Map @kqid into the user-namespace specified by @targ and
61  *      return the resulting qid.
62  *
63  *      There is always a mapping into the initial user_namespace.
64  *
65  *      If @kqid has no mapping in @targ (qid_t)-1 is returned.
66  */
67 qid_t from_kqid(struct user_namespace *targ, struct kqid kqid)
68 {
69         switch (kqid.type) {
70         case USRQUOTA:
71                 return from_kuid(targ, kqid.uid);
72         case GRPQUOTA:
73                 return from_kgid(targ, kqid.gid);
74         case PRJQUOTA:
75                 return from_kprojid(targ, kqid.projid);
76         default:
77                 BUG();
78         }
79 }
80 EXPORT_SYMBOL(from_kqid);
81
82 /**
83  *      from_kqid_munged - Create a qid from a kqid user-namespace pair.
84  *      @targ: The user namespace we want a qid in.
85  *      @kqid: The kernel internal quota identifier to start with.
86  *
87  *      Map @kqid into the user-namespace specified by @targ and
88  *      return the resulting qid.
89  *
90  *      There is always a mapping into the initial user_namespace.
91  *
92  *      Unlike from_kqid from_kqid_munged never fails and always
93  *      returns a valid projid.  This makes from_kqid_munged
94  *      appropriate for use in places where failing to provide
95  *      a qid_t is not a good option.
96  *
97  *      If @kqid has no mapping in @targ the kqid.type specific
98  *      overflow identifier is returned.
99  */
100 qid_t from_kqid_munged(struct user_namespace *targ, struct kqid kqid)
101 {
102         switch (kqid.type) {
103         case USRQUOTA:
104                 return from_kuid_munged(targ, kqid.uid);
105         case GRPQUOTA:
106                 return from_kgid_munged(targ, kqid.gid);
107         case PRJQUOTA:
108                 return from_kprojid_munged(targ, kqid.projid);
109         default:
110                 BUG();
111         }
112 }
113 EXPORT_SYMBOL(from_kqid_munged);
114
115 /**
116  *      qid_valid - Report if a valid value is stored in a kqid.
117  *      @qid: The kernel internal quota identifier to test.
118  */
119 bool qid_valid(struct kqid qid)
120 {
121         switch (qid.type) {
122         case USRQUOTA:
123                 return uid_valid(qid.uid);
124         case GRPQUOTA:
125                 return gid_valid(qid.gid);
126         case PRJQUOTA:
127                 return projid_valid(qid.projid);
128         default:
129                 BUG();
130         }
131 }
132 EXPORT_SYMBOL(qid_valid);