patch-2.6.6-vs1.9.0
[linux-2.6.git] / ipc / shm.c
1 /*
2  * linux/ipc/shm.c
3  * Copyright (C) 1992, 1993 Krishna Balasubramanian
4  *       Many improvements/fixes by Bruno Haible.
5  * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
6  * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
7  *
8  * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
9  * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
10  * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
11  * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
12  * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
13  * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
14  * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
15  *
16  */
17
18 #include <linux/config.h>
19 #include <linux/slab.h>
20 #include <linux/mm.h>
21 #include <linux/hugetlb.h>
22 #include <linux/shm.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/mman.h>
26 #include <linux/proc_fs.h>
27 #include <linux/shmem_fs.h>
28 #include <linux/security.h>
29 #include <asm/uaccess.h>
30
31 #include "util.h"
32
33 #define shm_flags       shm_perm.mode
34
35 static struct file_operations shm_file_operations;
36 static struct vm_operations_struct shm_vm_ops;
37
38 static struct ipc_ids shm_ids;
39
40 #define shm_lock(id)    ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
41 #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
42 #define shm_get(id)     ((struct shmid_kernel*)ipc_get(&shm_ids,id))
43 #define shm_buildid(id, seq) \
44         ipc_buildid(&shm_ids, id, seq)
45
46 static int newseg (key_t key, int shmflg, size_t size);
47 static void shm_open (struct vm_area_struct *shmd);
48 static void shm_close (struct vm_area_struct *shmd);
49 #ifdef CONFIG_PROC_FS
50 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
51 #endif
52
53 size_t  shm_ctlmax = SHMMAX;
54 size_t  shm_ctlall = SHMALL;
55 int     shm_ctlmni = SHMMNI;
56
57 static int shm_tot; /* total number of shared memory pages */
58
59 void __init shm_init (void)
60 {
61         ipc_init_ids(&shm_ids, 1);
62 #ifdef CONFIG_PROC_FS
63         create_proc_read_entry("sysvipc/shm", 0, 0, sysvipc_shm_read_proc, NULL);
64 #endif
65 }
66
67 static inline int shm_checkid(struct shmid_kernel *s, int id)
68 {
69         if (ipc_checkid(&shm_ids,&s->shm_perm,id))
70                 return -EIDRM;
71         return 0;
72 }
73
74 static inline struct shmid_kernel *shm_rmid(int id)
75 {
76         return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
77 }
78
79 static inline int shm_addid(struct shmid_kernel *shp)
80 {
81         return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni+1);
82 }
83
84
85
86 static inline void shm_inc (int id) {
87         struct shmid_kernel *shp;
88
89         if(!(shp = shm_lock(id)))
90                 BUG();
91         shp->shm_atim = get_seconds();
92         shp->shm_lprid = current->tgid;
93         shp->shm_nattch++;
94         shm_unlock(shp);
95 }
96
97 /* This is called by fork, once for every shm attach. */
98 static void shm_open (struct vm_area_struct *shmd)
99 {
100         shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
101 }
102
103 /*
104  * shm_destroy - free the struct shmid_kernel
105  *
106  * @shp: struct to free
107  *
108  * It has to be called with shp and shm_ids.sem locked,
109  * but returns with shp unlocked and freed.
110  */
111 static void shm_destroy (struct shmid_kernel *shp)
112 {
113         shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
114         shm_rmid (shp->id);
115         shm_unlock(shp);
116         if (!is_file_hugepages(shp->shm_file))
117                 shmem_lock(shp->shm_file, 0);
118         fput (shp->shm_file);
119         security_shm_free(shp);
120         ipc_rcu_free(shp, sizeof(struct shmid_kernel));
121 }
122
123 /*
124  * remove the attach descriptor shmd.
125  * free memory for segment if it is marked destroyed.
126  * The descriptor has already been removed from the current->mm->mmap list
127  * and will later be kfree()d.
128  */
129 static void shm_close (struct vm_area_struct *shmd)
130 {
131         struct file * file = shmd->vm_file;
132         int id = file->f_dentry->d_inode->i_ino;
133         struct shmid_kernel *shp;
134
135         down (&shm_ids.sem);
136         /* remove from the list of attaches of the shm segment */
137         if(!(shp = shm_lock(id)))
138                 BUG();
139         shp->shm_lprid = current->tgid;
140         shp->shm_dtim = get_seconds();
141         shp->shm_nattch--;
142         if(shp->shm_nattch == 0 &&
143            shp->shm_flags & SHM_DEST)
144                 shm_destroy (shp);
145         else
146                 shm_unlock(shp);
147         up (&shm_ids.sem);
148 }
149
150 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
151 {
152         file_accessed(file);
153         vma->vm_ops = &shm_vm_ops;
154         shm_inc(file->f_dentry->d_inode->i_ino);
155         return 0;
156 }
157
158 static struct file_operations shm_file_operations = {
159         .mmap   = shm_mmap
160 };
161
162 static struct vm_operations_struct shm_vm_ops = {
163         .open   = shm_open,     /* callback for a new vm-area open */
164         .close  = shm_close,    /* callback for when the vm-area is released */
165         .nopage = shmem_nopage,
166 };
167
168 static int newseg (key_t key, int shmflg, size_t size)
169 {
170         int error;
171         struct shmid_kernel *shp;
172         int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
173         struct file * file;
174         char name[13];
175         int id;
176
177         if (size < SHMMIN || size > shm_ctlmax)
178                 return -EINVAL;
179
180         if (shm_tot + numpages >= shm_ctlall)
181                 return -ENOSPC;
182
183         shp = ipc_rcu_alloc(sizeof(*shp));
184         if (!shp)
185                 return -ENOMEM;
186
187         shp->shm_perm.key = key;
188         shp->shm_perm.xid = current->xid;
189         shp->shm_flags = (shmflg & S_IRWXUGO);
190
191         shp->shm_perm.security = NULL;
192         error = security_shm_alloc(shp);
193         if (error) {
194                 ipc_rcu_free(shp, sizeof(*shp));
195                 return error;
196         }
197
198         if (shmflg & SHM_HUGETLB)
199                 file = hugetlb_zero_setup(size);
200         else {
201                 sprintf (name, "SYSV%08x", key);
202                 file = shmem_file_setup(name, size, VM_ACCOUNT);
203         }
204         error = PTR_ERR(file);
205         if (IS_ERR(file))
206                 goto no_file;
207
208         error = -ENOSPC;
209         id = shm_addid(shp);
210         if(id == -1) 
211                 goto no_id;
212
213         shp->shm_cprid = current->tgid;
214         shp->shm_lprid = 0;
215         shp->shm_atim = shp->shm_dtim = 0;
216         shp->shm_ctim = get_seconds();
217         shp->shm_segsz = size;
218         shp->shm_nattch = 0;
219         shp->id = shm_buildid(id,shp->shm_perm.seq);
220         shp->shm_file = file;
221         file->f_dentry->d_inode->i_ino = shp->id;
222         if (shmflg & SHM_HUGETLB)
223                 set_file_hugepages(file);
224         else
225                 file->f_op = &shm_file_operations;
226         shm_tot += numpages;
227         shm_unlock(shp);
228         return shp->id;
229
230 no_id:
231         fput(file);
232 no_file:
233         security_shm_free(shp);
234         ipc_rcu_free(shp, sizeof(*shp));
235         return error;
236 }
237
238 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
239 {
240         struct shmid_kernel *shp;
241         int err, id = 0;
242
243         down(&shm_ids.sem);
244         if (key == IPC_PRIVATE) {
245                 err = newseg(key, shmflg, size);
246         } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
247                 if (!(shmflg & IPC_CREAT))
248                         err = -ENOENT;
249                 else
250                         err = newseg(key, shmflg, size);
251         } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
252                 err = -EEXIST;
253         } else {
254                 shp = shm_lock(id);
255                 if(shp==NULL)
256                         BUG();
257                 if (shp->shm_segsz < size)
258                         err = -EINVAL;
259                 else if (ipcperms(&shp->shm_perm, shmflg))
260                         err = -EACCES;
261                 else {
262                         int shmid = shm_buildid(id, shp->shm_perm.seq);
263                         err = security_shm_associate(shp, shmflg);
264                         if (!err)
265                                 err = shmid;
266                 }
267                 shm_unlock(shp);
268         }
269         up(&shm_ids.sem);
270
271         return err;
272 }
273
274 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
275 {
276         switch(version) {
277         case IPC_64:
278                 return copy_to_user(buf, in, sizeof(*in));
279         case IPC_OLD:
280             {
281                 struct shmid_ds out;
282
283                 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
284                 out.shm_segsz   = in->shm_segsz;
285                 out.shm_atime   = in->shm_atime;
286                 out.shm_dtime   = in->shm_dtime;
287                 out.shm_ctime   = in->shm_ctime;
288                 out.shm_cpid    = in->shm_cpid;
289                 out.shm_lpid    = in->shm_lpid;
290                 out.shm_nattch  = in->shm_nattch;
291
292                 return copy_to_user(buf, &out, sizeof(out));
293             }
294         default:
295                 return -EINVAL;
296         }
297 }
298
299 struct shm_setbuf {
300         uid_t   uid;
301         gid_t   gid;
302         mode_t  mode;
303 };      
304
305 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
306 {
307         switch(version) {
308         case IPC_64:
309             {
310                 struct shmid64_ds tbuf;
311
312                 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
313                         return -EFAULT;
314
315                 out->uid        = tbuf.shm_perm.uid;
316                 out->gid        = tbuf.shm_perm.gid;
317                 out->mode       = tbuf.shm_flags;
318
319                 return 0;
320             }
321         case IPC_OLD:
322             {
323                 struct shmid_ds tbuf_old;
324
325                 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
326                         return -EFAULT;
327
328                 out->uid        = tbuf_old.shm_perm.uid;
329                 out->gid        = tbuf_old.shm_perm.gid;
330                 out->mode       = tbuf_old.shm_flags;
331
332                 return 0;
333             }
334         default:
335                 return -EINVAL;
336         }
337 }
338
339 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
340 {
341         switch(version) {
342         case IPC_64:
343                 return copy_to_user(buf, in, sizeof(*in));
344         case IPC_OLD:
345             {
346                 struct shminfo out;
347
348                 if(in->shmmax > INT_MAX)
349                         out.shmmax = INT_MAX;
350                 else
351                         out.shmmax = (int)in->shmmax;
352
353                 out.shmmin      = in->shmmin;
354                 out.shmmni      = in->shmmni;
355                 out.shmseg      = in->shmseg;
356                 out.shmall      = in->shmall; 
357
358                 return copy_to_user(buf, &out, sizeof(out));
359             }
360         default:
361                 return -EINVAL;
362         }
363 }
364
365 static void shm_get_stat(unsigned long *rss, unsigned long *swp) 
366 {
367         int i;
368
369         *rss = 0;
370         *swp = 0;
371
372         for (i = 0; i <= shm_ids.max_id; i++) {
373                 struct shmid_kernel *shp;
374                 struct inode *inode;
375
376                 shp = shm_get(i);
377                 if(!shp)
378                         continue;
379
380                 inode = shp->shm_file->f_dentry->d_inode;
381
382                 if (is_file_hugepages(shp->shm_file)) {
383                         struct address_space *mapping = inode->i_mapping;
384                         *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
385                 } else {
386                         struct shmem_inode_info *info = SHMEM_I(inode);
387                         spin_lock(&info->lock);
388                         *rss += inode->i_mapping->nrpages;
389                         *swp += info->swapped;
390                         spin_unlock(&info->lock);
391                 }
392         }
393 }
394
395 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
396 {
397         struct shm_setbuf setbuf;
398         struct shmid_kernel *shp;
399         int err, version;
400
401         if (cmd < 0 || shmid < 0) {
402                 err = -EINVAL;
403                 goto out;
404         }
405
406         version = ipc_parse_version(&cmd);
407
408         switch (cmd) { /* replace with proc interface ? */
409         case IPC_INFO:
410         {
411                 struct shminfo64 shminfo;
412
413                 err = security_shm_shmctl(NULL, cmd);
414                 if (err)
415                         return err;
416
417                 memset(&shminfo,0,sizeof(shminfo));
418                 shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
419                 shminfo.shmmax = shm_ctlmax;
420                 shminfo.shmall = shm_ctlall;
421
422                 shminfo.shmmin = SHMMIN;
423                 if(copy_shminfo_to_user (buf, &shminfo, version))
424                         return -EFAULT;
425                 /* reading a integer is always atomic */
426                 err= shm_ids.max_id;
427                 if(err<0)
428                         err = 0;
429                 goto out;
430         }
431         case SHM_INFO:
432         {
433                 struct shm_info shm_info;
434
435                 err = security_shm_shmctl(NULL, cmd);
436                 if (err)
437                         return err;
438
439                 memset(&shm_info,0,sizeof(shm_info));
440                 down(&shm_ids.sem);
441                 shm_info.used_ids = shm_ids.in_use;
442                 shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
443                 shm_info.shm_tot = shm_tot;
444                 shm_info.swap_attempts = 0;
445                 shm_info.swap_successes = 0;
446                 err = shm_ids.max_id;
447                 up(&shm_ids.sem);
448                 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
449                         err = -EFAULT;
450                         goto out;
451                 }
452
453                 err = err < 0 ? 0 : err;
454                 goto out;
455         }
456         case SHM_STAT:
457         case IPC_STAT:
458         {
459                 struct shmid64_ds tbuf;
460                 int result;
461                 memset(&tbuf, 0, sizeof(tbuf));
462                 shp = shm_lock(shmid);
463                 if(shp==NULL) {
464                         err = -EINVAL;
465                         goto out;
466                 } else if(cmd==SHM_STAT) {
467                         err = -EINVAL;
468                         if (shmid > shm_ids.max_id)
469                                 goto out_unlock;
470                         result = shm_buildid(shmid, shp->shm_perm.seq);
471                 } else {
472                         err = shm_checkid(shp,shmid);
473                         if(err)
474                                 goto out_unlock;
475                         result = 0;
476                 }
477                 err=-EACCES;
478                 if (ipcperms (&shp->shm_perm, S_IRUGO))
479                         goto out_unlock;
480                 err = security_shm_shmctl(shp, cmd);
481                 if (err)
482                         goto out_unlock;
483                 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
484                 tbuf.shm_segsz  = shp->shm_segsz;
485                 tbuf.shm_atime  = shp->shm_atim;
486                 tbuf.shm_dtime  = shp->shm_dtim;
487                 tbuf.shm_ctime  = shp->shm_ctim;
488                 tbuf.shm_cpid   = shp->shm_cprid;
489                 tbuf.shm_lpid   = shp->shm_lprid;
490                 if (!is_file_hugepages(shp->shm_file))
491                         tbuf.shm_nattch = shp->shm_nattch;
492                 else
493                         tbuf.shm_nattch = file_count(shp->shm_file) - 1;
494                 shm_unlock(shp);
495                 if(copy_shmid_to_user (buf, &tbuf, version))
496                         err = -EFAULT;
497                 else
498                         err = result;
499                 goto out;
500         }
501         case SHM_LOCK:
502         case SHM_UNLOCK:
503         {
504 /* Allow superuser to lock segment in memory */
505 /* Should the pages be faulted in here or leave it to user? */
506 /* need to determine interaction with current->swappable */
507                 if (!capable(CAP_IPC_LOCK)) {
508                         err = -EPERM;
509                         goto out;
510                 }
511
512                 shp = shm_lock(shmid);
513                 if(shp==NULL) {
514                         err = -EINVAL;
515                         goto out;
516                 }
517                 err = shm_checkid(shp,shmid);
518                 if(err)
519                         goto out_unlock;
520
521                 err = security_shm_shmctl(shp, cmd);
522                 if (err)
523                         goto out_unlock;
524                 
525                 if(cmd==SHM_LOCK) {
526                         if (!is_file_hugepages(shp->shm_file))
527                                 shmem_lock(shp->shm_file, 1);
528                         shp->shm_flags |= SHM_LOCKED;
529                 } else {
530                         if (!is_file_hugepages(shp->shm_file))
531                                 shmem_lock(shp->shm_file, 0);
532                         shp->shm_flags &= ~SHM_LOCKED;
533                 }
534                 shm_unlock(shp);
535                 goto out;
536         }
537         case IPC_RMID:
538         {
539                 /*
540                  *      We cannot simply remove the file. The SVID states
541                  *      that the block remains until the last person
542                  *      detaches from it, then is deleted. A shmat() on
543                  *      an RMID segment is legal in older Linux and if 
544                  *      we change it apps break...
545                  *
546                  *      Instead we set a destroyed flag, and then blow
547                  *      the name away when the usage hits zero.
548                  */
549                 down(&shm_ids.sem);
550                 shp = shm_lock(shmid);
551                 err = -EINVAL;
552                 if (shp == NULL) 
553                         goto out_up;
554                 err = shm_checkid(shp, shmid);
555                 if(err)
556                         goto out_unlock_up;
557
558                 if (current->euid != shp->shm_perm.uid &&
559                     current->euid != shp->shm_perm.cuid && 
560                     !capable(CAP_SYS_ADMIN)) {
561                         err=-EPERM;
562                         goto out_unlock_up;
563                 }
564
565                 err = security_shm_shmctl(shp, cmd);
566                 if (err)
567                         goto out_unlock_up;
568
569                 if (shp->shm_nattch){
570                         shp->shm_flags |= SHM_DEST;
571                         /* Do not find it any more */
572                         shp->shm_perm.key = IPC_PRIVATE;
573                         shm_unlock(shp);
574                 } else
575                         shm_destroy (shp);
576                 up(&shm_ids.sem);
577                 goto out;
578         }
579
580         case IPC_SET:
581         {
582                 if (copy_shmid_from_user (&setbuf, buf, version)) {
583                         err = -EFAULT;
584                         goto out;
585                 }
586                 down(&shm_ids.sem);
587                 shp = shm_lock(shmid);
588                 err=-EINVAL;
589                 if(shp==NULL)
590                         goto out_up;
591                 err = shm_checkid(shp,shmid);
592                 if(err)
593                         goto out_unlock_up;
594                 err=-EPERM;
595                 if (current->euid != shp->shm_perm.uid &&
596                     current->euid != shp->shm_perm.cuid && 
597                     !capable(CAP_SYS_ADMIN)) {
598                         goto out_unlock_up;
599                 }
600
601                 err = security_shm_shmctl(shp, cmd);
602                 if (err)
603                         goto out_unlock_up;
604                 
605                 shp->shm_perm.uid = setbuf.uid;
606                 shp->shm_perm.gid = setbuf.gid;
607                 shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
608                         | (setbuf.mode & S_IRWXUGO);
609                 shp->shm_ctim = get_seconds();
610                 break;
611         }
612
613         default:
614                 err = -EINVAL;
615                 goto out;
616         }
617
618         err = 0;
619 out_unlock_up:
620         shm_unlock(shp);
621 out_up:
622         up(&shm_ids.sem);
623         goto out;
624 out_unlock:
625         shm_unlock(shp);
626 out:
627         return err;
628 }
629
630 /*
631  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
632  *
633  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
634  * "raddr" thing points to kernel space, and there has to be a wrapper around
635  * this.
636  */
637 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
638 {
639         struct shmid_kernel *shp;
640         unsigned long addr;
641         unsigned long size;
642         struct file * file;
643         int    err;
644         unsigned long flags;
645         unsigned long prot;
646         unsigned long o_flags;
647         int acc_mode;
648         void *user_addr;
649
650         if (shmid < 0) {
651                 err = -EINVAL;
652                 goto out;
653         } else if ((addr = (ulong)shmaddr)) {
654                 if (addr & (SHMLBA-1)) {
655                         if (shmflg & SHM_RND)
656                                 addr &= ~(SHMLBA-1);       /* round down */
657                         else
658 #ifndef __ARCH_FORCE_SHMLBA
659                                 if (addr & ~PAGE_MASK)
660 #endif
661                                         return -EINVAL;
662                 }
663                 flags = MAP_SHARED | MAP_FIXED;
664         } else {
665                 if ((shmflg & SHM_REMAP))
666                         return -EINVAL;
667
668                 flags = MAP_SHARED;
669         }
670
671         if (shmflg & SHM_RDONLY) {
672                 prot = PROT_READ;
673                 o_flags = O_RDONLY;
674                 acc_mode = S_IRUGO;
675         } else {
676                 prot = PROT_READ | PROT_WRITE;
677                 o_flags = O_RDWR;
678                 acc_mode = S_IRUGO | S_IWUGO;
679         }
680
681         /*
682          * We cannot rely on the fs check since SYSV IPC does have an
683          * additional creator id...
684          */
685         shp = shm_lock(shmid);
686         if(shp == NULL) {
687                 err = -EINVAL;
688                 goto out;
689         }
690         err = shm_checkid(shp,shmid);
691         if (err) {
692                 shm_unlock(shp);
693                 goto out;
694         }
695         if (ipcperms(&shp->shm_perm, acc_mode)) {
696                 shm_unlock(shp);
697                 err = -EACCES;
698                 goto out;
699         }
700
701         err = security_shm_shmat(shp, shmaddr, shmflg);
702         if (err) {
703                 shm_unlock(shp);
704                 return err;
705         }
706                 
707         file = shp->shm_file;
708         size = i_size_read(file->f_dentry->d_inode);
709         shp->shm_nattch++;
710         shm_unlock(shp);
711
712         down_write(&current->mm->mmap_sem);
713         if (addr && !(shmflg & SHM_REMAP)) {
714                 user_addr = ERR_PTR(-EINVAL);
715                 if (find_vma_intersection(current->mm, addr, addr + size))
716                         goto invalid;
717                 /*
718                  * If shm segment goes below stack, make sure there is some
719                  * space left for the stack to grow (at least 4 pages).
720                  */
721                 if (addr < current->mm->start_stack &&
722                     addr > current->mm->start_stack - size - PAGE_SIZE * 5)
723                         goto invalid;
724         }
725                 
726         user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
727
728 invalid:
729         up_write(&current->mm->mmap_sem);
730
731         down (&shm_ids.sem);
732         if(!(shp = shm_lock(shmid)))
733                 BUG();
734         shp->shm_nattch--;
735         if(shp->shm_nattch == 0 &&
736            shp->shm_flags & SHM_DEST)
737                 shm_destroy (shp);
738         else
739                 shm_unlock(shp);
740         up (&shm_ids.sem);
741
742         *raddr = (unsigned long) user_addr;
743         err = 0;
744         if (IS_ERR(user_addr))
745                 err = PTR_ERR(user_addr);
746 out:
747         return err;
748 }
749
750 /*
751  * detach and kill segment if marked destroyed.
752  * The work is done in shm_close.
753  */
754 asmlinkage long sys_shmdt(char __user *shmaddr)
755 {
756         struct mm_struct *mm = current->mm;
757         struct vm_area_struct *vma, *next;
758         unsigned long addr = (unsigned long)shmaddr;
759         loff_t size = 0;
760         int retval = -EINVAL;
761
762         down_write(&mm->mmap_sem);
763
764         /*
765          * This function tries to be smart and unmap shm segments that
766          * were modified by partial mlock or munmap calls:
767          * - It first determines the size of the shm segment that should be
768          *   unmapped: It searches for a vma that is backed by shm and that
769          *   started at address shmaddr. It records it's size and then unmaps
770          *   it.
771          * - Then it unmaps all shm vmas that started at shmaddr and that
772          *   are within the initially determined size.
773          * Errors from do_munmap are ignored: the function only fails if
774          * it's called with invalid parameters or if it's called to unmap
775          * a part of a vma. Both calls in this function are for full vmas,
776          * the parameters are directly copied from the vma itself and always
777          * valid - therefore do_munmap cannot fail. (famous last words?)
778          */
779         /*
780          * If it had been mremap()'d, the starting address would not
781          * match the usual checks anyway. So assume all vma's are
782          * above the starting address given.
783          */
784         vma = find_vma(mm, addr);
785
786         while (vma) {
787                 next = vma->vm_next;
788
789                 /*
790                  * Check if the starting address would match, i.e. it's
791                  * a fragment created by mprotect() and/or munmap(), or it
792                  * otherwise it starts at this address with no hassles.
793                  */
794                 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
795                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
796
797
798                         size = vma->vm_file->f_dentry->d_inode->i_size;
799                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
800                         /*
801                          * We discovered the size of the shm segment, so
802                          * break out of here and fall through to the next
803                          * loop that uses the size information to stop
804                          * searching for matching vma's.
805                          */
806                         retval = 0;
807                         vma = next;
808                         break;
809                 }
810                 vma = next;
811         }
812
813         /*
814          * We need look no further than the maximum address a fragment
815          * could possibly have landed at. Also cast things to loff_t to
816          * prevent overflows and make comparisions vs. equal-width types.
817          */
818         while (vma && (loff_t)(vma->vm_end - addr) <= size) {
819                 next = vma->vm_next;
820
821                 /* finding a matching vma now does not alter retval */
822                 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
823                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
824
825                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
826                 vma = next;
827         }
828
829         up_write(&mm->mmap_sem);
830         return retval;
831 }
832
833 #ifdef CONFIG_PROC_FS
834 static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
835 {
836         off_t pos = 0;
837         off_t begin = 0;
838         int i, len = 0;
839
840         down(&shm_ids.sem);
841         len += sprintf(buffer, "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n");
842
843         for(i = 0; i <= shm_ids.max_id; i++) {
844                 struct shmid_kernel* shp;
845
846                 shp = shm_lock(i);
847                 if (shp) {
848 #define SMALL_STRING "%10d %10d  %4o %10u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
849 #define BIG_STRING   "%10d %10d  %4o %21u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
850                         char *format;
851
852                         if (!vx_check(shp->shm_perm.xid, VX_IDENT)) {
853                                 shm_unlock(shp);
854                                 continue;       
855                         }
856                         if (sizeof(size_t) <= sizeof(int))
857                                 format = SMALL_STRING;
858                         else
859                                 format = BIG_STRING;
860                         len += sprintf(buffer + len, format,
861                                 shp->shm_perm.key,
862                                 shm_buildid(i, shp->shm_perm.seq),
863                                 shp->shm_flags,
864                                 shp->shm_segsz,
865                                 shp->shm_cprid,
866                                 shp->shm_lprid,
867                                 is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
868                                 shp->shm_perm.uid,
869                                 shp->shm_perm.gid,
870                                 shp->shm_perm.cuid,
871                                 shp->shm_perm.cgid,
872                                 shp->shm_atim,
873                                 shp->shm_dtim,
874                                 shp->shm_ctim);
875                         shm_unlock(shp);
876
877                         pos += len;
878                         if(pos < offset) {
879                                 len = 0;
880                                 begin = pos;
881                         }
882                         if(pos > offset + length)
883                                 goto done;
884                 }
885         }
886         *eof = 1;
887 done:
888         up(&shm_ids.sem);
889         *start = buffer + (offset - begin);
890         len -= (offset - begin);
891         if(len > length)
892                 len = length;
893         if(len < 0)
894                 len = 0;
895         return len;
896 }
897 #endif