ioctl.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:12k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/fs/umsdos/ioctl.c
  3.  *
  4.  *  Written 1993 by Jacques Gelinas
  5.  *
  6.  *  Extended MS-DOS ioctl directory handling functions
  7.  */
  8. #include <asm/uaccess.h>
  9. #include <linux/errno.h>
  10. #include <linux/mm.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/fs.h>
  14. #include <linux/msdos_fs.h>
  15. #include <linux/umsdos_fs.h>
  16. struct UMSDOS_DIR_ONCE {
  17. struct dirent *ent;
  18. int count;
  19. };
  20. /*
  21.  * Record a single entry the first call.
  22.  * Return -EINVAL the next one.
  23.  */
  24. static int umsdos_ioctl_fill (
  25.      void *buf,
  26.      const char *name,
  27.      int name_len,
  28.      loff_t offset,
  29.      ino_t ino,
  30.      unsigned type)
  31. {
  32. int ret = -EINVAL;
  33. struct UMSDOS_DIR_ONCE *d = (struct UMSDOS_DIR_ONCE *) buf;
  34. if (d->count == 0) {
  35. copy_to_user (d->ent->d_name, name, name_len);
  36. put_user ('', d->ent->d_name + name_len);
  37. put_user (name_len, &d->ent->d_reclen);
  38. put_user (ino, &d->ent->d_ino);
  39. put_user (offset, &d->ent->d_off);
  40. d->count = 1;
  41. ret = 0;
  42. }
  43. return ret;
  44. }
  45. /*
  46.  * Perform special function on a directory
  47.  */
  48. /* #Specification: ioctl / prototypes
  49.  * The official prototype for the umsdos ioctl on directory
  50.  * is:
  51.  * 
  52.  * int ioctl (
  53.  * int fd,          // File handle of the directory
  54.  * int cmd, // command
  55.  * struct umsdos_ioctl *data)
  56.  * 
  57.  * The struct and the commands are defined in linux/umsdos_fs.h.
  58.  * 
  59.  * umsdos_progs/umsdosio.c provide an interface in C++ to all
  60.  * these ioctl. umsdos_progs/udosctl is a small utility showing
  61.  * all this.
  62.  * 
  63.  * These ioctl generally allow one to work on the EMD or the
  64.  * DOS directory independently. These are essential to implement
  65.  * the synchronise.
  66.  */
  67. int UMSDOS_ioctl_dir(struct inode *dir, struct file *filp, unsigned int cmd,
  68. unsigned long data_ptr)
  69. {
  70. struct dentry *dentry = filp->f_dentry;
  71. struct umsdos_ioctl *idata = (struct umsdos_ioctl *) data_ptr;
  72. int ret;
  73. struct umsdos_ioctl data;
  74. Printk(("UMSDOS_ioctl_dir: %s/%s, cmd=%d, data=%08lxn",
  75. dentry->d_parent->d_name.name, dentry->d_name.name, cmd, data_ptr));
  76. /* forward non-umsdos ioctls - this hopefully doesn't cause conflicts */
  77. if (cmd != UMSDOS_GETVERSION
  78.     && cmd != UMSDOS_READDIR_DOS
  79.     && cmd != UMSDOS_READDIR_EMD
  80.     && cmd != UMSDOS_INIT_EMD
  81.     && cmd != UMSDOS_CREAT_EMD
  82.     && cmd != UMSDOS_RENAME_DOS
  83.     && cmd != UMSDOS_UNLINK_EMD
  84.     && cmd != UMSDOS_UNLINK_DOS
  85.     && cmd != UMSDOS_RMDIR_DOS
  86.     && cmd != UMSDOS_STAT_DOS
  87.     && cmd != UMSDOS_DOS_SETUP)
  88. return fat_dir_ioctl (dir, filp, cmd, data_ptr);
  89. /* #Specification: ioctl / access
  90.  * Only root (effective id) is allowed to do IOCTL on directory
  91.  * in UMSDOS. EPERM is returned for other user.
  92.  */
  93. /*
  94.  * Well, not all cases require write access, but it simplifies
  95.  * the code, and let's face it, there is only one client (umssync)
  96.  * for all this.
  97.  */
  98. ret = verify_area (VERIFY_WRITE, (void *) data_ptr, 
  99. sizeof (struct umsdos_ioctl));
  100. if (ret < 0)
  101. goto out;
  102. ret = -EPERM;
  103. if (current->euid != 0 && cmd != UMSDOS_GETVERSION)
  104. goto out;
  105. ret = -EINVAL;
  106. if (cmd == UMSDOS_GETVERSION) {
  107. /* #Specification: ioctl / UMSDOS_GETVERSION
  108.  * The field version and release of the structure
  109.  * umsdos_ioctl are filled with the version and release
  110.  * number of the fs code in the kernel. This will allow
  111.  * some form of checking. Users won't be able to run
  112.  * incompatible utility such as the synchroniser (umssync).
  113.  * umsdos_progs/umsdosio.c enforce this checking.
  114.  * 
  115.  * Return always 0.
  116.  */
  117. put_user (UMSDOS_VERSION, &idata->version);
  118. put_user (UMSDOS_RELEASE, &idata->release);
  119. ret = 0;
  120. goto out;
  121. }
  122. if (cmd == UMSDOS_READDIR_DOS) {
  123. /* #Specification: ioctl / UMSDOS_READDIR_DOS
  124.  * One entry is read from the DOS directory at the current
  125.  * file position. The entry is put as is in the dos_dirent
  126.  * field of struct umsdos_ioctl.
  127.  * 
  128.  * Return > 0 if success.
  129.  */
  130. struct UMSDOS_DIR_ONCE bufk;
  131. bufk.count = 0;
  132. bufk.ent = &idata->dos_dirent;
  133. fat_readdir (filp, &bufk, umsdos_ioctl_fill);
  134. ret = bufk.count == 1 ? 1 : 0;
  135. goto out;
  136. }
  137. if (cmd == UMSDOS_READDIR_EMD) {
  138. /* #Specification: ioctl / UMSDOS_READDIR_EMD
  139.  * One entry is read from the EMD at the current
  140.  * file position. The entry is put as is in the umsdos_dirent
  141.  * field of struct umsdos_ioctl. The corresponding mangled
  142.  * DOS entry name is put in the dos_dirent field.
  143.  * 
  144.  * All entries are read including hidden links. Blank
  145.  * entries are skipped.
  146.  * 
  147.  * Return > 0 if success.
  148.  */
  149. struct dentry *demd;
  150. loff_t pos = filp->f_pos;
  151. /* The absence of the EMD is simply seen as an EOF */
  152. demd = umsdos_get_emd_dentry(dentry);
  153. ret = PTR_ERR(demd);
  154. if (IS_ERR(demd))
  155. goto out;
  156. ret = 0;
  157. if (!demd->d_inode)
  158. goto read_dput;
  159. while (pos < demd->d_inode->i_size) {
  160. off_t f_pos = pos;
  161. struct umsdos_dirent entry;
  162. struct umsdos_info info;
  163. ret = umsdos_emd_dir_readentry (demd, &pos, &entry);
  164. if (ret == -ENAMETOOLONG) {
  165. printk (KERN_INFO "Fixing EMD entry with invalid size -- zeroing outn");
  166. memset (&info, 0, sizeof (info));
  167. info.f_pos = f_pos;
  168. info.recsize = UMSDOS_REC_SIZE;
  169. ret = umsdos_writeentry (dentry, &info, 1);
  170. continue;
  171. }
  172. if (ret)
  173. break;
  174. if (entry.name_len <= 0)
  175. continue;
  176. umsdos_parse (entry.name, entry.name_len, &info);
  177. info.f_pos = f_pos;
  178. umsdos_manglename (&info);
  179. ret = -EFAULT;
  180. if (copy_to_user (&idata->umsdos_dirent, &entry,
  181. sizeof (entry)))
  182. break;
  183. if (copy_to_user (&idata->dos_dirent.d_name,
  184. info.fake.fname,
  185.   info.fake.len + 1))
  186. break;
  187. ret = entry.name_len;
  188. break;
  189. }
  190. /* update the original f_pos */
  191. filp->f_pos = pos;
  192. read_dput:
  193. d_drop(demd);
  194. dput(demd);
  195. goto out;
  196. }
  197. if (cmd == UMSDOS_INIT_EMD) {
  198. /* #Specification: ioctl / UMSDOS_INIT_EMD
  199.  * The UMSDOS_INIT_EMD command makes sure the EMD
  200.  * exists for a directory. If it does not, it is
  201.  * created. Also, it makes sure the directory function
  202.  * table (struct inode_operations) is set to the UMSDOS
  203.  * semantic. This mean that umssync may be applied to
  204.  * an "opened" msdos directory, and it will change behavior
  205.  * on the fly.
  206.  * 
  207.  * Return 0 if success.
  208.  */
  209. ret = umsdos_make_emd(dentry);
  210. Printk(("UMSDOS_ioctl_dir: INIT_EMD %s/%s, ret=%dn",
  211. dentry->d_parent->d_name.name, dentry->d_name.name, ret));
  212. umsdos_setup_dir (dentry);
  213. goto out;
  214. }
  215. ret = -EFAULT;
  216. if (copy_from_user (&data, idata, sizeof (data)))
  217. goto out;
  218. if (cmd == UMSDOS_CREAT_EMD) {
  219. /* #Specification: ioctl / UMSDOS_CREAT_EMD
  220.  * The umsdos_dirent field of the struct umsdos_ioctl is used
  221.  * as is to create a new entry in the EMD of the directory.
  222.  * The DOS directory is not modified.
  223.  * No validation is done (yet).
  224.  * 
  225.  * Return 0 if success.
  226.  */
  227. struct umsdos_info info;
  228. /* This makes sure info.entry and info in general
  229.  * is correctly initialised
  230.  */
  231. memcpy (&info.entry, &data.umsdos_dirent,
  232. sizeof (data.umsdos_dirent));
  233. umsdos_parse (data.umsdos_dirent.name
  234.     ,data.umsdos_dirent.name_len, &info);
  235. ret = umsdos_newentry (dentry, &info);
  236. goto out;
  237. }
  238. else if (cmd == UMSDOS_RENAME_DOS) {
  239. struct dentry *old_dentry, *new_dentry; /* FIXME */
  240. /* #Specification: ioctl / UMSDOS_RENAME_DOS
  241.  * A file or directory is renamed in a DOS directory
  242.  * (not moved across directory). The source name
  243.  * is in the dos_dirent.name field and the destination
  244.  * is in umsdos_dirent.name field.
  245.  * 
  246.  * This ioctl allows umssync to rename a mangled file
  247.  * name before syncing it back in the EMD.
  248.  */
  249. old_dentry = umsdos_lookup_dentry (dentry, 
  250. data.dos_dirent.d_name,
  251. data.dos_dirent.d_reclen ,1);
  252. ret = PTR_ERR(old_dentry);
  253. if (IS_ERR(old_dentry))
  254. goto out;
  255. new_dentry = umsdos_lookup_dentry (dentry,
  256. data.umsdos_dirent.name,
  257. data.umsdos_dirent.name_len, 1);
  258. ret = PTR_ERR(new_dentry);
  259. if (!IS_ERR(new_dentry)) {
  260. printk("umsdos_ioctl: renaming %s/%s to %s/%sn",
  261. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  262. new_dentry->d_parent->d_name.name, new_dentry->d_name.name);
  263. ret = msdos_rename (dir, old_dentry, dir, new_dentry);
  264. d_drop(new_dentry);
  265. d_drop(old_dentry);
  266. dput(new_dentry);
  267. }
  268. dput(old_dentry);
  269. goto out;
  270. }
  271. else if (cmd == UMSDOS_UNLINK_EMD) {
  272. /* #Specification: ioctl / UMSDOS_UNLINK_EMD
  273.  * The umsdos_dirent field of the struct umsdos_ioctl is used
  274.  * as is to remove an entry from the EMD of the directory.
  275.  * No validation is done (yet). The mode field is used
  276.  * to validate S_ISDIR or S_ISREG.
  277.  * 
  278.  * Return 0 if success.
  279.  */
  280. struct umsdos_info info;
  281. /* This makes sure info.entry and info in general
  282.  * is correctly initialised
  283.  */
  284. memcpy (&info.entry, &data.umsdos_dirent,
  285. sizeof (data.umsdos_dirent));
  286. umsdos_parse (data.umsdos_dirent.name,
  287. data.umsdos_dirent.name_len, &info);
  288. ret = umsdos_delentry (dentry, &info,
  289. S_ISDIR (data.umsdos_dirent.mode));
  290. if (ret) {
  291. printk(KERN_WARNING
  292. "umsdos_ioctl: delentry %s/%s failed, ret=%dn",
  293. dentry->d_name.name, info.entry.name, ret);
  294. }
  295. goto out;
  296. }
  297. else if (cmd == UMSDOS_UNLINK_DOS) {
  298. struct dentry *temp;
  299. /* #Specification: ioctl / UMSDOS_UNLINK_DOS
  300.  * The dos_dirent field of the struct umsdos_ioctl is used to
  301.  * execute a msdos_unlink operation. The d_name and d_reclen
  302.  * fields are used.
  303.  * 
  304.  * Return 0 if success.
  305.  */
  306. temp = umsdos_lookup_dentry(dentry, data.dos_dirent.d_name,
  307. data.dos_dirent.d_reclen, 1);
  308. ret = PTR_ERR(temp);
  309. if (IS_ERR(temp))
  310. goto out;
  311. ret = -ENOENT;
  312. if (temp->d_inode) {
  313. ret = -EISDIR;
  314. if (!S_ISDIR(temp->d_inode->i_mode))
  315. ret = msdos_unlink (dir, temp);
  316. if (!ret)
  317. d_delete(temp);
  318. }
  319. dput (temp);
  320. goto out;
  321. }
  322. else if (cmd == UMSDOS_RMDIR_DOS) {
  323. struct dentry *temp;
  324. /* #Specification: ioctl / UMSDOS_RMDIR_DOS
  325.  * The dos_dirent field of the struct umsdos_ioctl is used to
  326.  * execute a msdos_rmdir operation. The d_name and d_reclen
  327.  * fields are used.
  328.  * 
  329.  * Return 0 if success.
  330.  */
  331. temp = umsdos_lookup_dentry(dentry, data.dos_dirent.d_name,
  332.     data.dos_dirent.d_reclen, 1);
  333. ret = PTR_ERR(temp);
  334. if (IS_ERR(temp))
  335. goto out;
  336. ret = -ENOENT;
  337. if (temp->d_inode) {
  338. ret = -ENOTDIR;
  339. if (S_ISDIR(temp->d_inode->i_mode))
  340. ret = msdos_rmdir (dir, temp);
  341. if (!ret)
  342. d_delete(temp);
  343. }
  344. dput (temp);
  345. goto out;
  346. } else if (cmd == UMSDOS_STAT_DOS) {
  347. /* #Specification: ioctl / UMSDOS_STAT_DOS
  348.  * The dos_dirent field of the struct umsdos_ioctl is
  349.  * used to execute a stat operation in the DOS directory.
  350.  * The d_name and d_reclen fields are used.
  351.  * 
  352.  * The following field of umsdos_ioctl.stat are filled.
  353.  * 
  354.  * st_ino,st_mode,st_size,st_atime,st_mtime,st_ctime,
  355.  * Return 0 if success.
  356.  */
  357. struct dentry *dret;
  358. struct inode *inode;
  359. dret = umsdos_lookup_dentry(dentry, data.dos_dirent.d_name,
  360.     data.dos_dirent.d_reclen, 1);
  361. ret = PTR_ERR(dret);
  362. if (IS_ERR(dret))
  363. goto out;
  364. ret = -ENOENT;
  365. inode = dret->d_inode;
  366. if (inode) {
  367. data.stat.st_ino = inode->i_ino;
  368. data.stat.st_mode = inode->i_mode;
  369. data.stat.st_size = inode->i_size;
  370. data.stat.st_atime = inode->i_atime;
  371. data.stat.st_ctime = inode->i_ctime;
  372. data.stat.st_mtime = inode->i_mtime;
  373. ret = -EFAULT;
  374. if (!copy_to_user (&idata->stat, &data.stat, 
  375. sizeof (data.stat)))
  376. ret = 0;
  377. }
  378. dput(dret);
  379. goto out;
  380. }
  381. else if (cmd == UMSDOS_DOS_SETUP) {
  382. /* #Specification: ioctl / UMSDOS_DOS_SETUP
  383.  * The UMSDOS_DOS_SETUP ioctl allow changing the
  384.  * default permission of the MS-DOS filesystem driver
  385.  * on the fly.  The MS-DOS driver applies global permissions
  386.  * to every file and directory. Normally these permissions
  387.  * are controlled by a mount option. This is not
  388.  * available for root partition, so a special utility
  389.  * (umssetup) is provided to do this, normally in
  390.  * /etc/rc.local.
  391.  * 
  392.  * Be aware that this applies ONLY to MS-DOS directories
  393.  * (those without EMD --linux-.---). Umsdos directory
  394.  * have independent (standard) permission for each
  395.  * and every file.
  396.  * 
  397.  * The field umsdos_dirent provide the information needed.
  398.  * umsdos_dirent.uid and gid sets the owner and group.
  399.  * umsdos_dirent.mode set the permissions flags.
  400.  */
  401. dir->i_sb->u.msdos_sb.options.fs_uid = data.umsdos_dirent.uid;
  402. dir->i_sb->u.msdos_sb.options.fs_gid = data.umsdos_dirent.gid;
  403. dir->i_sb->u.msdos_sb.options.fs_umask = data.umsdos_dirent.mode;
  404. ret = 0;
  405. }
  406. out:
  407. Printk (("ioctl %d, returning %dn", cmd, ret));
  408. return ret;
  409. }