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