From 0c6fdced777c33c7869c059c30e3359a08644bd9 Mon Sep 17 00:00:00 2001
From: Bruce Ashfield <bruce.ashfield@windriver.com>
Date: Sat, 31 Aug 2013 23:41:50 -0400
Subject: [PATCH 2/3] yaffs2: convert to kuid_t and kgid_t

Based on the openwrt patch:

  https://dev.openwrt.org/export/37617/trunk/target/linux/generic/patches-3.10/512-yaffs-3.5-convert-to-use-kuid_t-kgid_t.patch

To fix compliation issues, we convert the latest yaffs2 to use kuid_t and
kgid_t for kernels greater than 3.9.

Signed-off-by: Bruce Ashfield <bruce.ashfield@windriver.com>

empty message aborts the commit.
---
 fs/yaffs2/yaffs_attribs.c |    8 ++++----
 fs/yaffs2/yaffs_attribs.h |   43 +++++++++++++++++++++++++++++++++++++++++++
 fs/yaffs2/yaffs_vfs.c     |   44 +++++++++++++++++++++++++++++++++++---------
 3 files changed, 82 insertions(+), 13 deletions(-)

diff --git a/fs/yaffs2/yaffs_attribs.c b/fs/yaffs2/yaffs_attribs.c
index 3d778f2..fdc7f21 100644
--- a/fs/yaffs2/yaffs_attribs.c
+++ b/fs/yaffs2/yaffs_attribs.c
@@ -77,9 +77,9 @@ int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
 	if (valid & ATTR_MODE)
 		obj->yst_mode = attr->ia_mode;
 	if (valid & ATTR_UID)
-		obj->yst_uid = attr->ia_uid;
+		obj->yst_uid = ia_uid_read(attr);
 	if (valid & ATTR_GID)
-		obj->yst_gid = attr->ia_gid;
+		obj->yst_gid = ia_gid_read(attr);
 
 	if (valid & ATTR_ATIME)
 		obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
@@ -103,9 +103,9 @@ int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
 
 	attr->ia_mode = obj->yst_mode;
 	valid |= ATTR_MODE;
-	attr->ia_uid = obj->yst_uid;
+	ia_uid_write( attr, obj->yst_uid );
 	valid |= ATTR_UID;
-	attr->ia_gid = obj->yst_gid;
+	ia_gid_write( attr, obj->yst_gid );
 	valid |= ATTR_GID;
 
 	Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
diff --git a/fs/yaffs2/yaffs_attribs.h b/fs/yaffs2/yaffs_attribs.h
index 5b21b08..121d99d 100644
--- a/fs/yaffs2/yaffs_attribs.h
+++ b/fs/yaffs2/yaffs_attribs.h
@@ -25,4 +25,47 @@ void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c);
 int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr);
 int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr);
 
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
+static inline uid_t ia_uid_read(const struct iattr *iattr)
+{
+	return from_kuid(&init_user_ns, iattr->ia_uid);
+}
+
+static inline gid_t ia_gid_read(const struct iattr *iattr)
+{
+	return from_kgid(&init_user_ns, iattr->ia_gid);
+}
+
+static inline void ia_uid_write(struct iattr *iattr, uid_t uid)
+{
+	iattr->ia_uid = make_kuid(&init_user_ns, uid);
+}
+
+static inline void ia_gid_write(struct iattr *iattr, gid_t gid)
+{
+	iattr->ia_gid = make_kgid(&init_user_ns, gid);
+}
+#else
+static inline uid_t ia_uid_read(const struct iattr *iattr)
+{
+	return iattr->ia_uid;
+}
+
+static inline gid_t ia_gid_read(const struct iattr *inode)
+{
+	return iattr->ia_gid;
+}
+
+static inline void ia_uid_write(struct iattr *iattr, uid_t uid)
+{
+	iattr->ia_uid = uid;
+}
+
+static inline void ia_gid_write(struct iattr *iattr, gid_t gid)
+{
+	iattr->ia_gid = gid;
+}
+#endif
+
 #endif
diff --git a/fs/yaffs2/yaffs_vfs.c b/fs/yaffs2/yaffs_vfs.c
index 67050d4..929c2e9 100644
--- a/fs/yaffs2/yaffs_vfs.c
+++ b/fs/yaffs2/yaffs_vfs.c
@@ -711,6 +711,33 @@ static struct address_space_operations yaffs_file_address_operations = {
 #endif
 };
 
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
+#define YCRED_FSUID()	from_kuid(&init_user_ns, current_fsuid())
+#define YCRED_FSGID()	from_kgid(&init_user_ns, current_fsgid())
+#else
+#define YCRED_FSUID()	YCRED(current)->fsuid
+#define YCRED_FSGID()	YCRED(current)->fsgid
+
+static inline uid_t i_uid_read(const struct inode *inode)
+{
+	return inode->i_uid;
+}
+
+static inline gid_t i_gid_read(const struct inode *inode)
+{
+	return inode->i_gid;
+}
+
+static inline void i_uid_write(struct inode *inode, uid_t uid)
+{
+	inode->i_uid = uid;
+}
+
+static inline void i_gid_write(struct inode *inode, gid_t gid)
+{
+	inode->i_gid = gid;
+}
+#endif
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
 static int yaffs_file_flush(struct file *file, fl_owner_t id)
@@ -1225,9 +1252,9 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
 	struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
 
 	int error = -ENOSPC;
-	uid_t uid = YCRED(current)->fsuid;
-	gid_t gid =
-	    (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
+
+	uid_t uid = YCRED_FSUID();
+	gid_t gid = (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
 
 	if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
 		mode |= S_ISGID;
@@ -1424,9 +1451,8 @@ static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
 {
 	struct yaffs_obj *obj;
 	struct yaffs_dev *dev;
-	uid_t uid = YCRED(current)->fsuid;
-	gid_t gid =
-	    (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
+	uid_t uid = YCRED_FSUID();
+	gid_t gid = (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
 
 	yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
 
@@ -1829,8 +1855,8 @@ static void yaffs_fill_inode_from_obj(struct inode *inode,
 
 		inode->i_ino = obj->obj_id;
 		inode->i_mode = obj->yst_mode;
-		inode->i_uid = obj->yst_uid;
-		inode->i_gid = obj->yst_gid;
+		i_uid_write(inode, obj->yst_uid);
+		i_gid_write(inode, obj->yst_gid);
 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
 		inode->i_blksize = inode->i_sb->s_blocksize;
 #endif
@@ -1856,7 +1882,7 @@ static void yaffs_fill_inode_from_obj(struct inode *inode,
 
 		yaffs_trace(YAFFS_TRACE_OS,
 			"yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
-			inode->i_mode, inode->i_uid, inode->i_gid,
+			inode->i_mode, i_uid_read(inode), i_gid_read(inode),
 			inode->i_size, atomic_read(&inode->i_count));
 
 		switch (obj->yst_mode & S_IFMT) {
-- 
1.7.10.4

